{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 作业2 空间数据库创建和数据查询\n"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "姓名：郑昱笙"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "学号：3180102760"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**作业目的：**了解OGC SFA标准，了解开源对象关系数据库系统PostgreSQL及其空间扩展PostGIS，熟悉PostGIS空间函数帮助文档查询方法，熟悉PostgreSQL空间数据库建库和数据导入，掌握各种几何关系判断、空间分析及相关SQL操作，熟悉在QGIS和在线地图上展示与分析查询结果。\n",
    "\n",
    "**注意事项：**\n",
    "* SQL语句的错误输出为乱码时，修改SET client_encoding = 'GBK';或SET client_encoding = 'UTF-8';，重新连接数据库\n",
    "* Jupyter Notebook对SQL语句的错误提示较弱，可以先在pgAdmin 4上执行，查看详细的错误信息\n",
    "* 作业2总分60分，作业考察的题目后面标了具体分数，可以相互讨论思路，作业抄袭或雷同都要扣分\n",
    "* 作业2\\_学号\\_姓名.ipynb替换其中的学号和姓名，包含执行结果，学号.jpg、作业2\\_学号\\_姓名.ipynb和jsonData目录压缩为作业2\\_学号\\_姓名.rar/zip，**不要包含数据文件**，提交到学在浙大，作业2截止日期**2020.3.29**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. OGC Simple Feature Access标准\n",
    "\n",
    "<a href=\"http://www.opengeospatial.org/docs/is\" target=\"_blank\">Open Geospatial Consortium</a>的Simple Feature Access标准包含两个部分Part 1 <a href=\"http://portal.opengeospatial.org/files/?artifact_id=25355\" target=\"_blank\">Common architecture</a>和Part 2 <a href=\"http://portal.opengeospatial.org/files/?artifact_id=25354\" target=\"_blank\">SQL option</a>两部分，给出了地理空间几何类型及其SQL实现规范，建议阅读参考。\n",
    "\n",
    "#### Part I Common architecture的Introduction介绍如下：\n",
    "\n",
    "OpenGIS®简单要素访问规范(SFA)的本文部分，也叫做ISO 19125，描述了简单地理要素的常用架构。简单地理要素对象模型是计算平台无关，并使用UML表示法。基类Geometry包含子类Point，Curve，Surface和GeometryCollection。每个几何对象和一个空间参考系(Spatial Reference System)关联，空间参考系描述了几何对象的坐标空间。\n",
    "\n",
    "扩展几何模型包括特定的0，1和2维集合类，即MultiPoint、MultiLineString和MultiPolygon，他们分别用于对点、线和面集合的建模。MultiCurve和MultiSurface作为抽象超类，用于产生处理曲线和面集合的接口。\n",
    "\n",
    "#### Part 2 SQL option的Introduction介绍如下：\n",
    "OpenGIS®简单要素访问规范(SFA)的第二部分，也被称作ISO 19125，定义了标准的结构化查询语句(SQL)规范，支持通过SQL调用接口(SQL/CLI) (ISO/IEC 9075-3：2003)的要素集合的存储、检索、查询和更新。一个要素同时具有空间和非空间属性。空间属性是具有几何意义(geometry valued)，同时简单要素是基于2D或更少维度的几何实体(点、曲线和面)，在二维中顶点之间可以线性插值，三维中顶点可以平面插值。这一标准是基于定义在Part 1中的常用架构组件。\n",
    "\n",
    "在SQL实现中，单个类型的要素集合存储在一张要素表的具有几何意义的属性(列)。每个要素通常表示为这一要素表中的一行，通过标准SQL技术逻辑连接这一要素表和其他表。要素的非空间属性(列)的数据类型来自于SQL数据类型，包括SQL3的用户自定义类型(UDT)。要素的空间属性(列)的数据类型是基于本标准的SQL的几何数据类型。要素表模式可以通过两种SQL方式实现，基于SQL预定义数据类型的经典SQL关系模型，和基于附加几何类型的SQL。无论哪种实现，几何表示有一组SQL方法函数，支持几何行为和查询。\n",
    "\n",
    "在基于预定义数据类型的实现中，具有几何意义的列通过几何表中一个几何ID实现。几何数据存储在几何表中的一行或多行，这些行具有相同的几何ID作为他们的主键。几何表可以使用标准的SQL数值类型或SQL二进制类型实现；这两者的模式在这个标准中描述。\n",
    "\n",
    "术语“带几何类型的SQL”常用来指拓展了一套几何类型的SQL实现。在这种实现中，一个具有几何意义的列通过几何类型的列实现。拓展SQL实现类型系统的机制是通过用户自定义的类型来完成的。基于用户自定义类型的商用的SQL实现从1997年中期开始就已经存在，对于UDT定义、ISO标准也已经存在。是作为SQL类型来自这套几何类型的列来实现的。商业的支持用户定义类型支持的SQL实现从1997年中期开始就已经存在，。这个标准不是指特定的用户定义类型机制，但需要支持UDTS定义的接口标准。这些接口描述了ISO/IEC 13249-3中的SQL3 UDTs。\n",
    "\n",
    "<img src=\"polygon.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.1 请给出图(a)中灰色多边形的Well-Known Text (WKT) Representation。（1分）"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "POLYGON((2 2, 11 2, 11 12, 2 12, 2 2), (4 4, 7 4, 7 9, 4 9, 4 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.2 基于6.1.11.1的Polygon的assertions (the rules that define valid Polygons)，请分析图(b)中几何对象不能用a polygon表示的原因。（1分）"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "assertions e: the interoir of every polygon is a connected point set\n",
    "该图形的内部不是connected point set；\n",
    "assertions c: the Rings in the boundary of a Polygon may intersect at a Point but only as a tangent\n",
    "这里有两个边界共用了两个点"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.3 请给出图(c)中绿色多边形(A)和蓝色线(B)的Dimensionally Extended Nine-Intersection Model (DE-9IM)。（1分）"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "    I(B)  B(B) E(B)\n",
    "I(A)  1    0   2\n",
    "B(A)  0    -1   1\n",
    "E(A)  1    0   2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.4 当a.Relate(b,“T\\*T\\*\\*\\*T\\*\\*”)返回True时，请给出几何对象a和b所对应的空间关系。（1分）"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Overlaps"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.5 请给出空间关系Contains的九交矩阵(9IM)的字符串表示。（1分）"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "T*****FF*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. PostGIS实现了OGC SFA标准，使用相应空间类型和函数时，建议查询<a href=\"http://postgis.net/docs/reference.html\" target=\"_blank\">帮助文档</a>。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.1 请翻译ST_MakePoint函数在PostGIS帮助文档中的Name和Description小节内容。"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Name\n",
    "ST_MakePoint — Creates a 2D,3DZ or 4D point geometry.\n",
    "ST_MakePoint – 创建一个2D，3DZ或4D点几何。\n",
    "\n",
    "Description\n",
    "Creates a 2D,3DZ or 4D point geometry (geometry with measure). ST_MakePoint while not being OGC compliant is generally faster and more precise than ST_GeomFromText and ST_PointFromText. It is also easier to use if you have raw coordinates rather than WKT.\n",
    "创建一个2D，3DZ或4D点几何（几何和度量）。ST_MakePoint虽然并不符合OGC规范，但通常比ST_GeomFromText和ST_PointFromText更快和精确。如果你使用原始坐标，而不是WKT，ST_MakePoint也更容易使用。\n",
    "\n",
    "Note x is longitude and y is latitude\n",
    "注意x是经度，y是纬度。\n",
    "    \n",
    "Use ST_MakePointM if you need to make a point with x,y,m.\n",
    "如果你需要一个具有x，y和m值的点，使用ST_MakePointM\n",
    "\n",
    "This function supports 3d and will not drop the z-index.\n",
    "本函数支持三维，不会丢弃z-index."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.2 ST_Distance函数说明：\n",
    "* For geometry type Returns the 2D Cartesian distance between two geometries in projected units (based on spatial ref). \n",
    "* For geography type defaults to return minimum geodesic distance between two geographies in meters.\n",
    "\n",
    "在空间参考系4326下，使用ST_Distance(geometry(Point, 4326), geometry(LineString, 4326))计算距离，返回的是什么距离，单位是什么？（1分）"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "返回的是最小的二维笛卡尔距离，空间计算单位为投影单位（度）;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.3 基于帮助文档，请比较~=(操作符)、=(操作符)、ST_Equals和ST_OrderingEquals四个函数的异同。（1分）"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "boolean =( geometry A , geometry B )：仅将在所有方面完全相同，坐标相同，顺序相同的几何视为相等。\n",
    "boolean ~=( geometry A , geometry B );将边界框相同的几何要素视为相等。（PostGIS 1.5前测试实际相等性）\n",
    "boolean ST_Equals(geometry A, geometry B);几何在空间上相等则返回true，不考虑点的顺序。即 ST_Within（A，B）= true 且 ST_Within（B，A）= true 。\n",
    "boolean ST_OrderingEquals(geometry A, geometry B);如果几何相等且坐标顺序相同，则返回TRUE。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.4 ST_Distance(Point, Polygon) <= 10和ST_DWithin(Point, Polygon, 10)功能上等价，而效率差异较大。基于帮助文档，请分析效率差异的原因。（1分）"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "ST_DWithin uses a more short-circuit distance function which should make it more efficient for larger buffer regions.\n",
    "ST_DWithin使用更多的短路距离功能，对于较大的缓冲区，它应当更有效。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.5 基于帮助文档，请比较ST_DistanceSphere(geometry pointlonlatA, geometry pointlonlatB)、ST_Distance(geometry g1, geometry g2)与ST_DistanceSpheroid(geometry pointlonlatA, geometry pointlonlatB, spheroid measurement_spheroid)三个函数的异同。（1分）"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "ST_DistanceSphere(geometry pointlonlatA, geometry pointlonlatB)：\n",
    "    Returns minimum distance in meters between two lon/lat points. Uses a spherical earth and radius derived from the spheroid defined by the SRID. Faster than ST_DistanceSpheroid, but less accurate. PostGIS Versions prior to 1.5 only implemented for points.\n",
    "ST_DistanceSpheroid(geometry pointlonlatA, geometry pointlonlatB, spheroid measurement_spheroid)：\n",
    "    Returns minimum distance in meters between two lon/lat geometries given a particular spheroid.\n",
    "    This function does not look at the SRID of the geometry. It assumes the geometry coordinates are based on the provided spheroid.\n",
    "ST_Distance(geometry g1, geometry g2)：\n",
    "    For geometry types returns the minimum 2D Cartesian (planar) distance between two geometries, in projected units (spatial ref units).\n",
    "相同点：都是以几何类型作为参数，返回两个几何之间的距离；\n",
    "不同点：\n",
    "ST_DistanceSphere 以几何所定义的SRID椭球体进行计算，返回单位为米；比ST_DistanceSpheroid快；\n",
    "ST_DistanceSpheroid 需要另外提供椭球体信息进行计算，返回单位为米；\n",
    "ST_Distance 返回二维平面上的笛卡尔距离，单位是投影单位。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.6 哪个函数可以将MultiXXX转换XXX，如MultiPolygon转换获得多个Polygon？（1分）"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "select ST_Dump(ST_GeomFromText('MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 美国湖泊、城市、高速公路及其交通事故的空间数据库创建和查询\n",
    "\n",
    "通过pgAdmin 4在PostgreSQL数据库中创建hw2数据库，添加postgis扩展(create extension postgis)，并连接该数据库。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext sql\n",
    "from geom_display import display\n",
    "from geom_display import choroplethMap\n",
    "from geom_display import heatMap\n",
    "\n",
    "# display([result1, result2, ...], divId, zoom)对数组中所有的result数据进行几何展示，\n",
    "# result的关系类型至少包含(gid，geom，name)，zoom为放缩比例, name是在地图上描述geom的名词\n",
    "\n",
    "# choroplethMap(result, divId, zoom)对数组中所有的result数据进行主题地图展示，\n",
    "# result的关系类型至少包括(gid，geom, name, value)，zoom为放缩比例, name是在地图上描述geom的名词, value是用于映射颜色的数值\n",
    "\n",
    "# heatMap(result, divId, zoom)对数组中所有的result数据进行热力图展示，\n",
    "# result的关系类型至少包括(gid，geom，name)，zoom为放缩比例，name是在地图上描述geom的名词, 也可以给出value值，用于颜色映射，缺省都为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Done.\n",
      "Done.\n",
      "Done.\n",
      "Done.\n",
      "Done.\n",
      "Done.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql postgresql://postgres:postgres@localhost/hw2\n",
    "\n",
    "SET statement_timeout = 0;\n",
    "SET lock_timeout = 0;\n",
    "SET client_encoding = 'UTF-8'; \n",
    "SET standard_conforming_strings = on;\n",
    "SET check_function_bodies = false;\n",
    "SET client_min_messages = warning;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.1 通过PostGIS的shapefile导入工具，在PostgreSQL中导入美国accidents、highways和lakes的shapefile数据。（1分）\n",
    "\n",
    "美国高速公路交通事故数据来源于[美国交通局](https://www.transportation.gov/fastlane/2015-traffic-fatalities-data-has-just-been-released-call-action-download-and-analyze) [白宫新闻备份](https://obamawhitehouse.archives.gov/blog/2016/08/29/2015-traffic-fatalities-data-has-just-been-released-call-action-download-and-analyze)，STATE为美国56个州的ID，ST_CASE由州ID和交通事故编号组成，交通事故发生在county和city，时间为day, month, year, day_week, hour和minute，地点在latitude和longitud，是否酒驾drunk_dr，大于0为酒驾。地点latitude和longitud存在错误数据情况，如大于1000，忽略这类错误数据。\n",
    "\n",
    "注意：shapefile文件不能放在包含中文的路径下，usaccidents、ushighways和uslakes的空间参考系需更改为4326。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      "the number of highways is 233\n",
      "the number of lakes is 29\n",
      "the number of accidents is 32166\n",
      "the SRID of ushighways is 4326\n",
      "the SRID of uslakes is 4326\n",
      "the SRID of usaccidents is 4326\n"
     ]
    }
   ],
   "source": [
    "highway_num  = %sql select count(*) from ushighways;\n",
    "lake_num     = %sql select count(*) from uslakes;\n",
    "accident_num = %sql select count(*) from usaccidents;\n",
    "highway_srid = %sql select ST_SRID(geom) from ushighways limit 1;\n",
    "lake_srid    = %sql select ST_SRID(geom) from uslakes limit 1;\n",
    "accident_srid= %sql select ST_SRID(geom) from usaccidents limit 1;\n",
    "print('the number of highways is ' + str(highway_num[0][0]))\n",
    "print('the number of lakes is ' + str(lake_num[0][0]))\n",
    "print('the number of accidents is ' + str(accident_num[0][0]))\n",
    "print('the SRID of ushighways is ' + str(highway_srid[0][0]))\n",
    "print('the SRID of uslakes is ' + str(lake_srid[0][0]))\n",
    "print('the SRID of usaccidents is ' + str(accident_srid[0][0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>updategeometrysrid</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>public.usaccidents.geom SRID changed to 4326</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('public.usaccidents.geom SRID changed to 4326',)]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改usaccidents, ushighways和uslakes得SRID为4326\n",
    "%sql select UpdateGeometrySRID('ushighways', 'geom', 4326);\n",
    "%sql select UpdateGeometrySRID('uslakes', 'geom', 4326);\n",
    "%sql select UpdateGeometrySRID('usaccidents', 'geom', 4326);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.2 创建关系uscities(gid, name, state, latitude, longitude)，gid的数据类型为integer，name和state的数据类型为varchar(100)，latitude和longitude的数据类型为numeric。（2分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "Done.\n",
      "Done.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql drop table if exists uscities;\n",
    "create table uscities(\n",
    "        gid integer primary key, \n",
    "        name varchar(100),\n",
    "        state varchar(100),\n",
    "        latitude numeric,\n",
    "        longitude numeric\n",
    ");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.3 通过[copy语句](https://www.postgresql.org/docs/current/static/sql-copy.html)导入uscities数据，注意属性之间的分隔符。（1分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "679 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%sql copy uscities from  'D:\\\\works\\\\GIS\\\\hw2\\\\usdata\\\\uscity.txt' delimiter '#';"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "3 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>gid</th>\n",
       "        <th>name</th>\n",
       "        <th>state</th>\n",
       "        <th>latitude</th>\n",
       "        <th>longitude</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>588</td>\n",
       "        <td>Faribault</td>\n",
       "        <td>Minnesota</td>\n",
       "        <td>44.29048646960</td>\n",
       "        <td>-93.26801273780</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>589</td>\n",
       "        <td>Mankato</td>\n",
       "        <td>Minnesota</td>\n",
       "        <td>44.16362082830</td>\n",
       "        <td>-93.99915674010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>590</td>\n",
       "        <td>Albert Lea</td>\n",
       "        <td>Minnesota</td>\n",
       "        <td>43.64778668120</td>\n",
       "        <td>-93.36870426610</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(588, 'Faribault', 'Minnesota', Decimal('44.29048646960'), Decimal('-93.26801273780')),\n",
       " (589, 'Mankato', 'Minnesota', Decimal('44.16362082830'), Decimal('-93.99915674010')),\n",
       " (590, 'Albert Lea', 'Minnesota', Decimal('43.64778668120'), Decimal('-93.36870426610'))]"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%sql select * from uscities limit 3;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.4 对关系uscities增加几何属性列geom，根据每个城市的latitude和longtide，更新geom属性，注意空间参考系需与ushighways和uslakes相同。（2分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      "679 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql \n",
    "select AddGeometryColumn('uscities', 'geom', 4326, 'POINT', 2);\n",
    "UPDATE uscities SET geom = ST_SetSRID(ST_Point(longitude,latitude),4326);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.5 在QGIS中展示City图层、Highway图层、Lake图层和Accident图层，截图保存为学号.jpg，与本文件同一目录，修改下面的highways.png为你的学号，Shift+Enter能正确展示QGIS截图。可能由于浏览器图片缓存原因，修改后不能立即显示新图片，重新打开jupyter notebook验证图片是否正确显示。（1分）\n",
    "<img src=\"3180102760.jpg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.6 构造以下GIS分析与SQL查询，注意空间函数对Geometry和GeometryCollection的有效性。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.0 查询伊利湖(Erie)的边界，通过display函数在OpenStreetMap中展示该边界，display函数要求查询结果模式至少包含gid，name和geom属性。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map0' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map0.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new GeoMap('map0' ,6,0)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query = \"\"\"\n",
    "select gid, 'Lake Erie''s Boundary' as name, ST_Boundary(geom) as geom\n",
    "from uslakes\n",
    "where name like '%Erie%'\n",
    "\"\"\"\n",
    "result = %sql $query\n",
    "\n",
    "display([result], \"map0\", 6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.1 查询苏必利尔湖(Superior)几何数据中点的数目(the number of points in the geometry of 'Superior')（2分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>gid</th>\n",
       "        <th>name</th>\n",
       "        <th>number</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>26</td>\n",
       "        <td>Lake Superior&#x27;s number of point</td>\n",
       "        <td>166</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(26, \"Lake Superior's number of point\", 166)]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql \n",
    "select gid, 'Lake Superior''s number of point' as name, ST_NPoints(geom) as number\n",
    "from uslakes\n",
    "where name like '%Superior%';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.2 查询高速公路全称(full_name)为’I 278’的凸包，通过display函数在OpenStreetMap中展示该凸包，display函数要求查询结果模式至少包含gid，name和geom属性（2分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map1' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map1.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new GeoMap('map1' ,11,0)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query = \"\"\" \n",
    "select gid,  name, ST_ConvexHull(geom)  as geom\n",
    "from ushighways\n",
    "where full_name = 'I 278';\n",
    "\n",
    "\"\"\" \n",
    "result1 = %sql $query\n",
    "result2 = %sql select gid, geom, full_name as name from ushighways where full_name = 'I 278'\n",
    "\n",
    "display([result1, result2], \"map1\", 11)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.3 查询哪些湖中有岛，通过display函数在OpenStreetMap中展示这些湖，display函数要求查询结果模式至少包含gid，name和geom属性（2分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "4 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map2' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map2.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new GeoMap('map2' ,4,0)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query = \"\"\"\n",
    "select gid,  name, geom from uslakes where ST_NRings(geom) > 1;\n",
    "\"\"\"\n",
    "\n",
    "result = %sql $query\n",
    "\n",
    "display([result], \"map2\", 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.4 查询湖的面积属性是否准确(绝对误差小于1e-6)，列出面积属性不准确的湖及其误差，查询结果模式为(gid，name，error)（2分）<br/>\n",
    "**数据清洗与验证**：数据输入时，可能存在错误或误差，此时需要通过数据清理Data Cleaning，对数据进行验证和纠错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 348,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "3 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>gid</th>\n",
       "        <th>name</th>\n",
       "        <th>error</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>9</td>\n",
       "        <td>Great Salt Lake</td>\n",
       "        <td>0.10000000000274623</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>24</td>\n",
       "        <td>Lake Erie</td>\n",
       "        <td>0.00019999999795938805</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>29</td>\n",
       "        <td>Lake Oahe</td>\n",
       "        <td>0.09999999999773646</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(9, 'Great Salt Lake', 0.10000000000274623),\n",
       " (24, 'Lake Erie', 0.00019999999795938805),\n",
       " (29, 'Lake Oahe', 0.09999999999773646)]"
      ]
     },
     "execution_count": 348,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql \n",
    "select gid,  name, abs(ST_Area(geom) - shape_area) error from uslakes where abs(ST_Area(geom) - shape_area) >= 1e-6;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.5 查询最长的高速公路及其长度(单位为千米)，通过display函数在OpenStreetMap中展示该高速公路，查询结果模式为(gid，name，geom，length)，其中name为高速公路的full_name（2分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      "4648.205571504647\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map3' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map3.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new GeoMap('map3' ,4,0)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query = \"\"\"\n",
    "select gid, full_name as name, geom, ST_Length(geom::geography)/1000.0 length\n",
    "from ushighways order by length desc limit 1;\n",
    "\"\"\"\n",
    "\n",
    "result = %sql $query\n",
    "print(result[0]['length'])\n",
    "\n",
    "display([result], \"map3\", 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.6 查询与安大略湖(Ontario)的质心距离最近的城市，通过display函数在OpenStreetMap中展示该湖和城市，display函数要求查询结果模式至少包含gid，name和geom属性，其中城市的name为‘name in state’的格式（2分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map4' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map4.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new GeoMap('map4' ,6,0)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query = \"\"\" \n",
    "select gid, name || ' in ' || state as name, uc.geom,  ST_Distance(ST_Centroid(la.geom) , uc.geom) distance from uscities uc, \n",
    "(select geom from uslakes where name like '%Ontario%') la order by distance limit 1;\n",
    "\"\"\"\n",
    "\n",
    "result1 = %sql $query\n",
    "result2 = %sql select gid, name, geom from uslakes where name like '%Ontario%';\n",
    "\n",
    "display([result1, result2], \"map4\", 6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.7 查询距离ST_CASE = 10012交通事故最近的城市，查询返回距离最近的城市名'name in state'，不能使用关键词limit（2分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Gadsden in Alabama</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Gadsden in Alabama',)]"
      ]
     },
     "execution_count": 184,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql \n",
    "select uc.name || ' in ' || uc.state as name from uscities uc, usaccidents ua where st_case = 10012\n",
    "    and ST_Distance(uc.geom,ua.geom) = ( select min(ST_Distance(uc.geom,ua.geom)) from uscities uc, usaccidents ua where st_case = 10012);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.8 查询94号公路(gid=94)与哪些高速公路联通，不包括94号公路，求总长度（单位为千米），通过display函数在OpenStreetMap中展示这些高速公路，display函数要求查询结果模式至少包含gid，name和geom属性，其中name为高速公路的full_name（3分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "2 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map5' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map5.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new GeoMap('map5' ,5,0)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>sum</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2448.2577281149997</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(2448.2577281149997,)]"
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询联通的高速公路\n",
    "query = \"\"\"\n",
    " select gid, full_name as name, geom from ushighways where \n",
    " ST_Intersects((select geom from ushighways where gid = 94),geom) and gid != 94;\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "result1 = %sql $query\n",
    "result2 = %sql select gid,geom, full_name as name from ushighways where gid = 94\n",
    "\n",
    "display([result1, result2], \"map5\", 5)\n",
    "\n",
    "# 查询总长度\n",
    "query2 = \"\"\"\n",
    "  select sum(ST_Length(geom::geography)/1000.0) from ushighways where \n",
    " ST_Intersects((select geom from ushighways where gid = 94),geom) and gid != 94 ;\n",
    "\"\"\"\n",
    "\n",
    "%sql $query2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.9 查询与伊利湖(Erie)距离最近的高速公路，通过display函数在OpenStreetMap中展示该湖和高速公路，display函数要求查询结果模式至少包含gid，name和geom属性，其中高速公路的name为full_name（3分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 236,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map6' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map6.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new GeoMap('map6' ,4,0)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query = \"\"\"\n",
    "select gid, full_name as name, geom \n",
    "from ushighways order by\n",
    "ST_Distance((select geom from uslakes where name like '%Erie%'),geom) limit 1;\n",
    "\"\"\"\n",
    "\n",
    "result1 = %sql $query\n",
    "result2 = %sql select gid, name, geom from uslakes where name like '%Erie%'\n",
    "\n",
    "display([result1, result2], \"map6\", 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.10 查询哪个城市最偏僻，即离高速公路的距离最远，通过display函数在OpenStreetMap中展示最偏僻的城市和与其最近的高速公路，display函数要求查询结果模式至少包含gid，name和geom属性，其中高速公路的name为full_name（3分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map7' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map7.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new GeoMap('map7' ,4,0)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 查询最偏僻的城市\n",
    "query1 = \"\"\"\n",
    "select uc.gid, uc.name || ' in ' || uc.state as name, uc.geom, min(ST_Distance(uc.geom,hw.geom)) d \n",
    "from uscities uc , ushighways hw group by uc.gid order by d desc limit 1;\n",
    "\"\"\"\n",
    "\n",
    "result1 = %sql $query1\n",
    "\n",
    "# 查询与最偏僻的城市最近的高速公路\n",
    "query2 = \"\"\" \n",
    "select gid, full_name as name, geom from ushighways order by ST_Distance((select uc.geom  \n",
    "from uscities uc,ushighways hw group by uc.gid order by min(ST_Distance(uc.geom,hw.geom)) desc limit 1),geom) limit 1;\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "result2 = %sql $query2\n",
    "\n",
    "display([result1, result2], \"map7\", 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.11 查询哪些高速公路穿越湖，列出高速公路及其在湖中的长度，按长度从长到短排列，通过display函数在OpenStreetMap中展示这些高速公路和湖，display函数要求查询结果模式至少包含gid，name和geom属性， 其中高速公路的hname为full_name（3分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "7 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "6 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map8' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map8.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new GeoMap('map8' ,4,0)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "8 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>hgid</th>\n",
       "        <th>hname</th>\n",
       "        <th>lgid</th>\n",
       "        <th>lname</th>\n",
       "        <th>length</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>183</td>\n",
       "        <td>I 75</td>\n",
       "        <td>27</td>\n",
       "        <td>Lake Huron</td>\n",
       "        <td>8.130551864269865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>10</td>\n",
       "        <td>I 15</td>\n",
       "        <td>9</td>\n",
       "        <td>Great Salt Lake</td>\n",
       "        <td>7.843049183864646</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>200</td>\n",
       "        <td>I 81</td>\n",
       "        <td>28</td>\n",
       "        <td>Lake Ontario</td>\n",
       "        <td>6.419771479346665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>183</td>\n",
       "        <td>I 75</td>\n",
       "        <td>11</td>\n",
       "        <td>Lake Michigan</td>\n",
       "        <td>4.335250022249676</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>137</td>\n",
       "        <td>I 535</td>\n",
       "        <td>26</td>\n",
       "        <td>Lake Superior</td>\n",
       "        <td>3.009733197765361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>168</td>\n",
       "        <td>I 69</td>\n",
       "        <td>27</td>\n",
       "        <td>Lake Huron</td>\n",
       "        <td>0.635554789097042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>40</td>\n",
       "        <td>I 24</td>\n",
       "        <td>19</td>\n",
       "        <td>Lake Barkley</td>\n",
       "        <td>0.5994961944385678</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>139</td>\n",
       "        <td>I 55</td>\n",
       "        <td>11</td>\n",
       "        <td>Lake Michigan</td>\n",
       "        <td>0.008943501893995903</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(183, 'I 75', 27, 'Lake Huron', 8.130551864269865),\n",
       " (10, 'I 15', 9, 'Great Salt Lake', 7.843049183864646),\n",
       " (200, 'I 81', 28, 'Lake Ontario', 6.419771479346665),\n",
       " (183, 'I 75', 11, 'Lake Michigan', 4.335250022249676),\n",
       " (137, 'I 535', 26, 'Lake Superior', 3.009733197765361),\n",
       " (168, 'I 69', 27, 'Lake Huron', 0.635554789097042),\n",
       " (40, 'I 24', 19, 'Lake Barkley', 0.5994961944385678),\n",
       " (139, 'I 55', 11, 'Lake Michigan', 0.008943501893995903)]"
      ]
     },
     "execution_count": 197,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询穿越湖的公路\n",
    "query1 = \"\"\" \n",
    "select distinct hw.gid, full_name as name, hw.geom from ushighways hw, uslakes la where ST_Crosses(la.geom,hw.geom);\n",
    "\"\"\" \n",
    "\n",
    "result1 = %sql $query1\n",
    "\n",
    "# 查询被公路穿越的湖\n",
    "query2 = \"\"\" \n",
    "select distinct la.gid,  la.name, la.geom from ushighways hw, uslakes la where ST_Crosses(la.geom,hw.geom);\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "result2 = %sql $query2\n",
    "\n",
    "display([result1, result2], \"map8\", 4)\n",
    "\n",
    "# 查询高速公路在湖中的长度(hgid, hname, lgid, lname, length)\n",
    "query3 = \"\"\" \n",
    "select hw.gid hgid, full_name as hname, la.gid lgid, la.name lname,ST_Length(ST_Intersection(la.geom,hw.geom)::geography)/1000.0 length\n",
    "from ushighways hw, uslakes la where ST_Crosses(la.geom,hw.geom) order by length desc;\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "%sql $query3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.12 将交通事故与高速公路基于空间距离进行关联，即距离某高速公路小于500米，认为该交通事故发生在这条高速公路上，查询哪条高速公路上的交通事故最多？由于交通事故较多，完整的查询大约需要30分钟，可以使用ST_DWithin加速距离判断，同时仅考虑在8月和9月发生的交通事故。通过display函数在OpenStreetMap中展示这些高速公路和其关联的交通事故，display函数要求查询结果模式至少包含gid，name和geom属性，其中高速公路的name为full_name（4分）<br/>\n",
    "**空间关联查询**：此类空间关联查询是数据挖掘中的常见方法，应用较为广泛，如[道路与车辆关联](https://www.csdn.net/article/2015-01-23/2823687-geographic-space-base-Hadoop)分析道路拥堵情况？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      "183\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "62 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map9' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map9.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new GeoMap('map9' ,4,1)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 查询满足题意的高速公路\n",
    "query1 = \"\"\"\n",
    "select hw.gid, hw.full_name as name, hw.geom, count(*) c from (select * from usaccidents where month = 8 or month = 9) ua, ushighways hw \n",
    "where ST_DWithin(hw.geom::geography, ua.geom::geography,500)  \n",
    "group by hw.gid order by c desc limit 1;\n",
    "\"\"\"\n",
    "\n",
    "result1 = %sql $query1\n",
    "print(result1[0].gid)\n",
    "\n",
    "# 查询该高速公路上的交通事故\n",
    "query2 = \"\"\" \n",
    "select ua.gid, hw1.name,ua.geom from\n",
    "(select hw.gid, hw.full_name as name, hw.geom, count(*) c \n",
    "from (select * from usaccidents where month = 8 or month = 9) ua, ushighways hw \n",
    "where ST_DWithin(hw.geom::geography, ua.geom::geography,500)  \n",
    "group by hw.gid order by c desc limit 1) hw1, (select * from usaccidents where month = 8 or month = 9) ua \n",
    "where ST_DWithin(hw1.geom::geography, ua.geom::geography,500);\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "result2 = %sql $query2\n",
    "\n",
    "display([result1, result2], \"map9\", 4, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.13 导入加州cal的shapefile文件，计算加州的经纬度范围，在该范围内，基于练习4网格生成方法构建$50\\times46$，即X方向50，Y方向46，网格与加州多边形求交，即边界上的方格仅保留与加州相交的部分，统计每个方格内的交通事故数目，通过choroplethMap进行可视化，choroplethMap函数要求查询结果模式至少包含gid，name，geom和value属性，其中value为其内的交通事故数目，可以通过with语句简化SQL语句（4分）<br/>\n",
    "**空间网格关联查询**：此类空间网格关联查询在实际应用中较为常见，如滴滴和Uber基于六边形网格统计打车人数，进行实时调价，[Uber Deck Grid and Hexagon layers](https://eng.uber.com/deck-gl-4-0/)\n",
    "<img src=\"hexagon.jpg\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>updategeometrysrid</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>public.cal.geom SRID changed to 4326</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('public.cal.geom SRID changed to 4326',)]"
      ]
     },
     "execution_count": 214,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%sql select * from cal;\n",
    "%sql select UpdateGeometrySRID('cal', 'geom', 4326);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "483 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map10' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map10.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new ChoroplethMap('map10' ,6,1)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query = \"\"\"\n",
    "select x || '0' || y as gid, x || ' ' || y as name, grid1.geom ,count(*) as value\n",
    "from usaccidents ua, \n",
    "(WITH \n",
    "    usext AS (\n",
    "        SELECT \n",
    "            ST_SetSRID(CAST(ST_Extent(geom) AS geometry),\n",
    "            4326) AS geom_ext, 50 AS x_gridcnt, 46 AS y_gridcnt\n",
    "        FROM cal\n",
    "    ),\n",
    "    grid_dim AS (\n",
    "        SELECT \n",
    "            (\n",
    "                ST_XMax(geom_ext)-ST_XMin(geom_ext)\n",
    "                ) / x_gridcnt AS g_width, \n",
    "            ST_XMin(geom_ext) AS xmin, ST_xmax(geom_ext) AS xmax,\n",
    "            (\n",
    "                ST_YMax(geom_ext)-ST_YMin(geom_ext)\n",
    "                ) / y_gridcnt AS g_height,     \n",
    "            ST_YMin(geom_ext) AS ymin, ST_YMax(geom_ext) AS ymax\n",
    "        FROM usext                                    \n",
    "    ), \n",
    "    grid AS (                    \n",
    "        SELECT \n",
    "            x, y, \n",
    "            ST_MakeEnvelope(  \n",
    "                xmin + (x - 1) * g_width, ymin + (y - 1) * g_height,  \n",
    "                xmin + x * g_width, ymin + y * g_height,\n",
    "                4326\n",
    "            ) AS grid_geom \n",
    "        FROM \n",
    "            (SELECT generate_series(1,x_gridcnt) FROM usext) AS x(x)    \n",
    "            CROSS JOIN \n",
    "            (SELECT generate_series(1,y_gridcnt) FROM usext) AS y(y) \n",
    "            CROSS JOIN \n",
    "            grid_dim                                                 \n",
    "    )   \n",
    "SELECT \n",
    "    g.x x, g.y y, \n",
    "    ST_Intersection(s.geom, grid_geom) AS geom                   \n",
    "FROM cal AS s INNER JOIN grid AS g \n",
    "ON ST_Intersects(s.geom,g.grid_geom)) grid1\n",
    "where ST_Within(ua.geom,grid1.geom)\n",
    "group by grid1.x,grid1.y,grid1.geom;\n",
    "\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "result = %sql $query\n",
    "\n",
    "choroplethMap(result, \"map10\", 6, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.6.14 查询在加州范围内的交通事故，通过heatMap进行可视化，heatMap函数要求查询结果模式至少包含gid，name和geom属性，其中name可为任意值，并对比ChoroplethMap与HeatMap在地理空间数据可视化方面的异同（3分）<br/>\n",
    "**数据可视化**：利用人眼的感知能力对数据进行交互的可视表达以增强认知的技术，是数据分析的有效手段，如[Uber数据可视化](http://dataunion.org/24227.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 345,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "2897 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map11' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map11.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new HeatMap('map11' ,4,1)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query = \"\"\"\n",
    "select gid, tway_id as name, geom from usaccidents where ST_Within(geom, (select geom from cal ));\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "result = %sql $query\n",
    "\n",
    "heatMap(result, \"map11\", 4, 1)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "ChoroplethMap与HeatMap在地理空间数据可视化方面的异同：\n",
    "同：都可以直观地表示出数据的地理空间分布特性，如疏密程度或频率高低等；\n",
    "不同：\n",
    "ChoroplethMap需要进行空间网格划分，相对绘制较为复杂，但在数据表现的准确性高；\n",
    "HeatMap可以简单地聚合大量数据，并使用渐进的色带来表现，相对更为直观，但在数据表现的准确性相对较低。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.7 酒驾交通事故分析\n",
    "\n",
    "美国交通局：We' re directly soliciting your help to better understand what these data are telling us. Whether you' re a non-profit, a tech company, or just a curious citizen wanting to contribute to the conversation in your local community, we want you to jump in and help us understand what the data are telling us. Some key questions worth exploring:\n",
    "* How might improving economic conditions around the country change how Americans are getting around? What models can we develop to identify communities that might be at a higher risk for fatal crashes?\n",
    "* How might climate change increase the risk of fatal crashes in a community? \n",
    "* How might we use studies of attitudes toward speeding, distracted driving, and seat belt use to better target marketing and behavioral change campaigns?\n",
    "* How might we monitor public health indicators and behavior risk indicators to target communities that might have a high prevalence of behaviors linked with fatal crashes (drinking, drug use/addiction, etc.)? What countermeasures should we create to address these issues?\n",
    "\n",
    "<img src=\"drunk.jpg\">\n",
    "\n",
    "美国交通局在2018年开展了[Visualize Transportation Safety](https://www.transportation.gov/solve4safety)可视化挑战赛。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.7.1 酒驾是否在周末更容易发生？构造SQL语句查询工作日平均每日酒驾事件数与周末平均每日酒驾事件数(avg_weekday_count, avg_weekend_count)，保留到小数点后4位，分析查询结果给出结论，注意中美文化差异中星期起始日的差别（3分）<br/>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>avg_weekday_count</th>\n",
       "        <th>avg_weekend_count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>17.8506</td>\n",
       "        <td>40.4135</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(Decimal('17.8506'), Decimal('40.4135'))]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql \n",
    "select round( ((select sum(count)::float from (select count(*) from usaccidents where drunk_dr > 0 and day_week between 2 and 6) s)/\n",
    "(select count(*)::float from (select day,month,year from usaccidents where day_week between 2 and 6 group by day,month,year) c))::numeric,4)\n",
    "avg_weekday_count,\n",
    "round( ((select sum(count)::float from (select count(*) from usaccidents where drunk_dr > 0 and day_week not between 2 and 6) s)/\n",
    "(select count(*)::float from (select day,month,year from usaccidents where day_week not between 2 and 6 group by day,month,year) c))::numeric,4)\n",
    "avg_weekend_count\n",
    ";"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "结论：由于周末平均每日酒驾事件数显著高于工作日平均每日酒驾事件数，因此可以得出酒驾是在周末更容易发生。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.7.2 (练习题) 酒驾交通事故在工作日和休息日在哪个时间段发生较多？构造SQL语句查询(hour, avg_weekday_count, avg_weekend_count)，保留到小数点后4位，分析查询结果给出结论"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 310,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "24 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>hour</th>\n",
       "        <th>avg_weekday_count</th>\n",
       "        <th>avg_weekend_count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>0.0</td>\n",
       "        <td>1.2261</td>\n",
       "        <td>3.0865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1.0</td>\n",
       "        <td>1.1916</td>\n",
       "        <td>3.7019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2.0</td>\n",
       "        <td>1.0881</td>\n",
       "        <td>4.2788</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>3.0</td>\n",
       "        <td>0.7318</td>\n",
       "        <td>3.2115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4.0</td>\n",
       "        <td>0.4406</td>\n",
       "        <td>1.7981</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5.0</td>\n",
       "        <td>0.3870</td>\n",
       "        <td>1.2885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6.0</td>\n",
       "        <td>0.3257</td>\n",
       "        <td>1.0481</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7.0</td>\n",
       "        <td>0.3295</td>\n",
       "        <td>0.7115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8.0</td>\n",
       "        <td>0.1609</td>\n",
       "        <td>0.3558</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>9.0</td>\n",
       "        <td>0.1762</td>\n",
       "        <td>0.3462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>10.0</td>\n",
       "        <td>0.1686</td>\n",
       "        <td>0.4135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>11.0</td>\n",
       "        <td>0.2720</td>\n",
       "        <td>0.4904</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>12.0</td>\n",
       "        <td>0.3027</td>\n",
       "        <td>0.4231</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>13.0</td>\n",
       "        <td>0.3908</td>\n",
       "        <td>0.6731</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>14.0</td>\n",
       "        <td>0.4176</td>\n",
       "        <td>0.7885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>15.0</td>\n",
       "        <td>0.5785</td>\n",
       "        <td>1.0288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>16.0</td>\n",
       "        <td>0.7165</td>\n",
       "        <td>1.1923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>17.0</td>\n",
       "        <td>0.8812</td>\n",
       "        <td>1.9135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>18.0</td>\n",
       "        <td>1.1303</td>\n",
       "        <td>1.9519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>19.0</td>\n",
       "        <td>1.1801</td>\n",
       "        <td>2.1154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>20.0</td>\n",
       "        <td>1.2644</td>\n",
       "        <td>2.4038</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>21.0</td>\n",
       "        <td>1.4674</td>\n",
       "        <td>2.3173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>22.0</td>\n",
       "        <td>1.4483</td>\n",
       "        <td>2.0577</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>23.0</td>\n",
       "        <td>1.3678</td>\n",
       "        <td>2.2788</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(0.0, Decimal('1.2261'), Decimal('3.0865')),\n",
       " (1.0, Decimal('1.1916'), Decimal('3.7019')),\n",
       " (2.0, Decimal('1.0881'), Decimal('4.2788')),\n",
       " (3.0, Decimal('0.7318'), Decimal('3.2115')),\n",
       " (4.0, Decimal('0.4406'), Decimal('1.7981')),\n",
       " (5.0, Decimal('0.3870'), Decimal('1.2885')),\n",
       " (6.0, Decimal('0.3257'), Decimal('1.0481')),\n",
       " (7.0, Decimal('0.3295'), Decimal('0.7115')),\n",
       " (8.0, Decimal('0.1609'), Decimal('0.3558')),\n",
       " (9.0, Decimal('0.1762'), Decimal('0.3462')),\n",
       " (10.0, Decimal('0.1686'), Decimal('0.4135')),\n",
       " (11.0, Decimal('0.2720'), Decimal('0.4904')),\n",
       " (12.0, Decimal('0.3027'), Decimal('0.4231')),\n",
       " (13.0, Decimal('0.3908'), Decimal('0.6731')),\n",
       " (14.0, Decimal('0.4176'), Decimal('0.7885')),\n",
       " (15.0, Decimal('0.5785'), Decimal('1.0288')),\n",
       " (16.0, Decimal('0.7165'), Decimal('1.1923')),\n",
       " (17.0, Decimal('0.8812'), Decimal('1.9135')),\n",
       " (18.0, Decimal('1.1303'), Decimal('1.9519')),\n",
       " (19.0, Decimal('1.1801'), Decimal('2.1154')),\n",
       " (20.0, Decimal('1.2644'), Decimal('2.4038')),\n",
       " (21.0, Decimal('1.4674'), Decimal('2.3173')),\n",
       " (22.0, Decimal('1.4483'), Decimal('2.0577')),\n",
       " (23.0, Decimal('1.3678'), Decimal('2.2788'))]"
      ]
     },
     "execution_count": 310,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql \n",
    "select ua.hour,\n",
    "round( ((select sum(count)::float from (select count(*) from usaccidents ua1 where ua1.hour = ua.hour and  ua1.drunk_dr > 0 and ua1.day_week between 2 and 6) s)/\n",
    "(select count(*)::float from (select day,month,year from usaccidents where day_week between 2 and 6 group by day,month,year) c))::numeric,4)\n",
    "avg_weekday_count,\n",
    "round( ((select sum(count)::float from (select count(*) from usaccidents ua1 where ua1.hour = ua.hour and  ua1.drunk_dr > 0 and ua1.day_week not between 2 and 6) s)/\n",
    "(select count(*)::float from (select day,month,year from usaccidents where day_week not between 2 and 6 group by day,month,year) c))::numeric,4)\n",
    "avg_weekend_count\n",
    "from usaccidents ua where hour between 0 and 23 group by hour order by hour;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 311,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "24 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<BarContainer object of 24 artists>"
      ]
     },
     "execution_count": 311,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAEXCAYAAACpuuMDAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzt3Xe8HVXV//HPyg29JEjCA6QQSgAhUi+hqgRBQn+oL6ogSIQfRYRHhEdEBKRZEBXEKEgTENEHgzRRKYKASWgSaihCRAwCgggCwfX7Y+1DZk5Om5Mz99wk3/frdV73zJy976w5ZdbsvaeYuyMiIlIxoNsBiIhI/6LEICIiOUoMIiKSo8QgIiI5SgwiIpKjxCAiIjlKDCIikqPEICIiOUoMIiKSo8QgIiI5A7sdQDuGDBnio0aN6nYYIiLzlKlTp/7d3Yc2KzdPJoZRo0YxZcqUbochIjJPMbM/t1JOXUkiIpKjxCAiIjlKDCIikqPEICIiOUoMIiKSo8QgIiI5SgwiIpKjxCAiIjnz5AluIiJ9ZdQJN7RU7rmzdig5kr6jFoOIiOQoMYiISI66kkREOmxe735Si0FERHKUGEREJKfUxGBmF5vZTDN7pEm5jczsfTPbo8x4RESkubJbDJcA4xsVMLMe4GzglpJjERGRFpSaGNz9TuDVJsWOAn4OzCwzFhERaU1XxxjMbBiwK3BhC2UnmNkUM5vy8ssvlx+ciMgCqtuDz98Gvuju7zcr6O4T3b3X3XuHDm16y1IREWlTt89j6AWuNjOAIcD2ZjbL3a/rblgiI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JdYnaqdNObH1hgblWkgjkryfVQtlliKOqjmv2A5+bOkXjardO0dj68/q3GVvh24Ga2UHAZI9LuJdZp1/ddlWJQaSBvthg96U2NsD9dv37YjltJpN+tZFvhxKDiIjkaIxBRERylBhERCRHiUFERHKUGERaYGajzOyRbsch0heUGES6JN1SUqTfUWIQaV2Pmf3QzKaZ2a/NbDEzW8/M7jWzh83s/9Lx9ZjZ7WbWm54PMbPn0vODzOxnZnY9cftTkX5HiUGkdaOB8919beIKm7sTd5r7oruvQ1yK+yst/J9NiRvybFVapCJzQYlBpHXPuvuD6flU4vaqg939jjTvUuJCe83c6u6vlhGgSCcoMYi07p3M8/eJ6wHVM4vZv69Fq177VyeDEuk0JQaR9r0OvGZmH03TBxDX3oe4t/CG6fkefRyXyFzRUREic+dA4EIzW5y4Bean0/xvANeY2QHEfYJF5hm6VpKIiOSoK0lERHKUGEREJEeJQUREcpQYREQkR4lBRERylBhERCRHiUFERHKUGEREJOf/A3aI/RbQgMk2AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query1 = \"\"\"\n",
    "select ua.hour, round( ((select sum(count)::float from (select count(*) from usaccidents ua1 where ua1.hour = ua.hour and  ua1.drunk_dr > 0 and ua1.day_week between 2 and 6) s)/\n",
    "(select count(*)::float from (select day,month,year from usaccidents where day_week between 2 and 6 group by day,month,year) c))::numeric,4) \n",
    "avg_weekday_count from usaccidents ua where hour between 0 and 23 group by hour order by hour;\n",
    "\"\"\"\n",
    "result1 = %sql $query1\n",
    "%matplotlib inline\n",
    "result1.bar()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "24 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<BarContainer object of 24 artists>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAEXCAYAAACpuuMDAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzt3XmcXFWZ//HPlw4JCJIwpJUlCQ2CgCJrEzYXQB3ZhJ+CTlSQIE5ECYsoCC6AuAwuozKgMhEcFpVFFA2r4igojoAJhDUiEREiKBEQRQQMPL8/zilzq1Lb7e7b3Ul/369XvbrurXPqPre66j73nHMXRQRmZmY1q4x0AGZmNro4MZiZWR0nBjMzq+PEYGZmdZwYzMysjhODmZnVGZbEIKlH0q2Srmjy2kxJSyQtyI93D0dMZmbW3LhhWs7RwEJgrRavXxwRs4cpFjMza6PyFoOkKcA+wNlVL8vMzAZvOLqSvgQcDzzfpswBkm6XdKmkqc0KSJolaV5+zKokUjMzQ1VeEkPSvsDeEfE+SbsBH4yIfRvKrAM8GRHPSDoceGtE7NHufSdPnhx9fX1VhW1mtlKaP3/+nyKit1O5qhPDfwAHA0uB1UhjDN+NiINalO8BHouIie3et7+/P+bNmzfU4ZqZrdQkzY+I/k7lKu1KiogTI2JKRPQBM4AfNyYFSesVJvcjDVKbmdkIGa6jkupIOhWYFxFzgaMk7UdqVTwGzByJmMzMLKm0K6kq7koyMytvVHQlmZnZiseJwczM6jgxmJlZHScGMzOr48RgZmZ1RuRw1bGg74Qruyp3/2n7VByJmVk5bjGYmVkdJwYzM6vjxGBmZnWcGMzMrI4Tg5mZ1XFiMDOzOk4MZmZWx4nBzMzqODGYmVmdYUkMknok3SrpiiavTZB0saRFkm6S1DccMZmZWXPD1WI4mta37DwMeDwiNgG+CHxmmGIyM7MmKk8MkqYA+wBntyiyP3Befn4p8FpJqjouMzNrbjhaDF8C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j7VrugBXqXJSnN87rM77EcrYkdT+26xJtHPP7b9pcXLFJnXeQuu3WKFHnnaTWactB4yZ1Dia1AkvtHA31Y8yc4GaWL6HwOVJTvod0PP/iLsrvnMu/JjqcVFSos2ue9aqI+GOJuEQ6OOIPXdapxbZ7u3VpsZzXRMTDXZbv+Hk1iWtcyTq75rjKrssqdPe/OZc0PvCvpK6cO9qVb1Ln0OjiTOPhqlOlMXutJBt7Il1n53ZSv++bOm18CuUnkS713PFM00KdtUj9/m2TQpO4DuiUFFrE1nZdWiynZVJoUr7j59UkrrJ11hrgurT93ygZD7yKtOc/o1NSaFGn7cZ6uOoMi5Fsrvjhx3A+KDlOVLb8ylZntMY1iDozKTmYO5rrVPlwV5KNKcXrSVVRfmWrM1rjGkid4nWKVoY6VXJiMDOzOh5jMDOzOk4MZmZWx4nBzMzqODGYmVkdJwazLkjqk3TnSMdhNhycGMxGSL61o9mo48Rg1r0eSV+TdJekH0paXdI2km6UdLuky/KN3pF0naT+/HyypPvz85mSvi3pctLtT81GHScGs+5tCnw5Il5OuvTyAaQ7zX0oIrYi3aPh5C7eZ2fSDXn2qCxSs0FwYjDr3m8jYkF+Pp90e9VJEXF9nnce6QqsnVwbEY9VEaDZUHBiMOveM4Xnz5EuFNfKUpb9vlZreO1vQxmU2VBzYjAbuCeAxyW9Kk8fTLopC6R7C2+fnx84zHGZDYqPijAbnEOAsyS9gHRv5EPz/M8Dl0g6mHQDebMVhi+iZ2ZmddyVZGZmdZwYzMysjhODmZnVcWIwM7M6TgxmZlbHicHMzOo4MZiZWZ3/D4lgUEfMpx/yAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query2 = \"\"\"\n",
    "select ua.hour, round( ((select sum(count)::float from (select count(*) from usaccidents ua1 where ua1.hour = ua.hour and  ua1.drunk_dr > 0 and ua1.day_week not between 2 and 6) s)/\n",
    "(select count(*)::float from (select day,month,year from usaccidents where day_week not between 2 and 6 group by day,month,year) c))::numeric,4) \n",
    "avg_weekend_count from usaccidents ua where hour between 0 and 23 group by hour order by hour;\n",
    "\"\"\"\n",
    "result2 = %sql $query2\n",
    "%matplotlib inline\n",
    "result2.bar()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "结论：在工作日酒驾发生的时段差异相对较小，集中在凌晨一点到三点和晚上六点至十二点的入夜时间段，其中晚六点至十二点的发生几率略高于凌晨一点到三点；在休息日则主要集中在凌晨十二点到三点发生，晚上六点至十二点发生几率相对中午时间段也较多，且仍高于工作日的发生几率。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.7.3 (练习题) 酒驾交通事故次数在每个小时上是否与总的交通事故次数成正比？在一天的哪些小时上，酒驾是交通事故的主要原因？构造SQL语句分析是否成正比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 319,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "24 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>hour</th>\n",
       "        <th>avg_drunk_count</th>\n",
       "        <th>avg_total_count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>0.0</td>\n",
       "        <td>1.7562</td>\n",
       "        <td>3.4301</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1.0</td>\n",
       "        <td>1.9068</td>\n",
       "        <td>3.2877</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2.0</td>\n",
       "        <td>1.9973</td>\n",
       "        <td>3.2548</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>3.0</td>\n",
       "        <td>1.4384</td>\n",
       "        <td>2.5644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4.0</td>\n",
       "        <td>0.8274</td>\n",
       "        <td>2.0301</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5.0</td>\n",
       "        <td>0.6438</td>\n",
       "        <td>2.7068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6.0</td>\n",
       "        <td>0.5315</td>\n",
       "        <td>3.2466</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7.0</td>\n",
       "        <td>0.4384</td>\n",
       "        <td>3.0986</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8.0</td>\n",
       "        <td>0.2164</td>\n",
       "        <td>2.4822</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>9.0</td>\n",
       "        <td>0.2247</td>\n",
       "        <td>2.5973</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>10.0</td>\n",
       "        <td>0.2384</td>\n",
       "        <td>2.7918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>11.0</td>\n",
       "        <td>0.3342</td>\n",
       "        <td>3.0603</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>12.0</td>\n",
       "        <td>0.3370</td>\n",
       "        <td>3.3014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>13.0</td>\n",
       "        <td>0.4712</td>\n",
       "        <td>3.7699</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>14.0</td>\n",
       "        <td>0.5233</td>\n",
       "        <td>4.0795</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>15.0</td>\n",
       "        <td>0.7068</td>\n",
       "        <td>4.5726</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>16.0</td>\n",
       "        <td>0.8521</td>\n",
       "        <td>4.4493</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>17.0</td>\n",
       "        <td>1.1753</td>\n",
       "        <td>5.0027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>18.0</td>\n",
       "        <td>1.3644</td>\n",
       "        <td>5.1452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>19.0</td>\n",
       "        <td>1.4466</td>\n",
       "        <td>4.8164</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>20.0</td>\n",
       "        <td>1.5890</td>\n",
       "        <td>5.0301</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>21.0</td>\n",
       "        <td>1.7096</td>\n",
       "        <td>4.9096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>22.0</td>\n",
       "        <td>1.6219</td>\n",
       "        <td>4.2137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>23.0</td>\n",
       "        <td>1.6274</td>\n",
       "        <td>3.6027</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(0.0, Decimal('1.7562'), Decimal('3.4301')),\n",
       " (1.0, Decimal('1.9068'), Decimal('3.2877')),\n",
       " (2.0, Decimal('1.9973'), Decimal('3.2548')),\n",
       " (3.0, Decimal('1.4384'), Decimal('2.5644')),\n",
       " (4.0, Decimal('0.8274'), Decimal('2.0301')),\n",
       " (5.0, Decimal('0.6438'), Decimal('2.7068')),\n",
       " (6.0, Decimal('0.5315'), Decimal('3.2466')),\n",
       " (7.0, Decimal('0.4384'), Decimal('3.0986')),\n",
       " (8.0, Decimal('0.2164'), Decimal('2.4822')),\n",
       " (9.0, Decimal('0.2247'), Decimal('2.5973')),\n",
       " (10.0, Decimal('0.2384'), Decimal('2.7918')),\n",
       " (11.0, Decimal('0.3342'), Decimal('3.0603')),\n",
       " (12.0, Decimal('0.3370'), Decimal('3.3014')),\n",
       " (13.0, Decimal('0.4712'), Decimal('3.7699')),\n",
       " (14.0, Decimal('0.5233'), Decimal('4.0795')),\n",
       " (15.0, Decimal('0.7068'), Decimal('4.5726')),\n",
       " (16.0, Decimal('0.8521'), Decimal('4.4493')),\n",
       " (17.0, Decimal('1.1753'), Decimal('5.0027')),\n",
       " (18.0, Decimal('1.3644'), Decimal('5.1452')),\n",
       " (19.0, Decimal('1.4466'), Decimal('4.8164')),\n",
       " (20.0, Decimal('1.5890'), Decimal('5.0301')),\n",
       " (21.0, Decimal('1.7096'), Decimal('4.9096')),\n",
       " (22.0, Decimal('1.6219'), Decimal('4.2137')),\n",
       " (23.0, Decimal('1.6274'), Decimal('3.6027'))]"
      ]
     },
     "execution_count": 319,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "select ua.hour,\n",
    "round( ((select sum(count)::float from (select count(*) from usaccidents ua1 where ua1.hour = ua.hour and  ua1.drunk_dr > 0 ) s)/\n",
    "(select count(*)::float from (select day,month,year from usaccidents group by day,month,year) c))::numeric,4)\n",
    "avg_drunk_count,\n",
    "round( ((select sum(count)::float from (select count(*) from usaccidents ua1 where ua1.hour = ua.hour ) s)/\n",
    "(select count(*)::float from (select day,month,year from usaccidents group by day,month,year) c))::numeric,4)\n",
    "avg_total_count \n",
    "from usaccidents ua where hour between 0 and 23 group by hour order by hour;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 339,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "24 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "24 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<BarContainer object of 24 artists>"
      ]
     },
     "execution_count": 339,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAEXCAYAAACu1P9TAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzt3Xm8HFWZ//HPkwSSECABEhQIEPadsISwjrKpbOLI4o8dxCGggCCbOoILMi6IC+ICQR2QVUBAdsFRYEARE2AQZBVBERmiOCgg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8IXEakIJXwRkYpQwpdKM7NpZvbAUMch0gt9fT1sAAABGUlEQVRK+CIFSz+rJ9J3lPBFYLSZnWtmD5rZzWY23sw2MrO7zOx+M7sqjQ/HzG41sxnp+WQzezI9P8TMLjeza4lfehLpO0r4InFfnG+6+3rEHRP3JH6Z66PuviFxu+RPdfB/tiR+6GT70iIVGQQlfBH4nbvfl57PJX5mcpK735bmnU/cXK2dW9z9+TICFCmCEr5I/NBIzRvEj7Y0M5+B/WZc3WsvFRmUSNGU8EUW9gLwVzP7lzR9IHHvc4jfTd00Pd+rx3GJDIpGE4g0djBwtpktRvwM4PvT/DOAy8zsQOJ3UEWGDd1LR0SkItSlIyJSEUr4IiIVoYQvIlIRSvgiIhWhhC8iUhFK+CIiFaGELyJSEUr4IiIV8f8BRtxuFxjXnk4AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "query1 = \"\"\"\n",
    "select ua.hour,\n",
    "round( ((select sum(count)::float from (select count(*) from usaccidents ua1 where ua1.hour = ua.hour and  ua1.drunk_dr > 0 ) s)/\n",
    "(select count(*)::float from (select day,month,year from usaccidents group by day,month,year) c))::numeric,4)\n",
    "avg_drunk_count\n",
    "from usaccidents ua where hour between 0 and 23 group by hour order by hour;\n",
    "\"\"\"\n",
    "query2 = \"\"\"\n",
    "select ua.hour,\n",
    "round( ((select sum(count)::float from (select count(*) from usaccidents ua1 where ua1.hour = ua.hour ) s)/\n",
    "(select count(*)::float from (select day,month,year from usaccidents group by day,month,year) c))::numeric,4)\n",
    "avg_total_count \n",
    "from usaccidents ua where hour between 0 and 23 group by hour order by hour;\n",
    "\"\"\"\n",
    "result1 = %sql $query1\n",
    "result2 = %sql $query2\n",
    "%matplotlib inline\n",
    "result2.bar()\n",
    "result1.bar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "如上图所示，橙色为酒驾交通事故每小时平均次数，蓝色为总平均次数；"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "结论：观察可知，\n",
    "酒驾交通事故次数在每个小时上不与总的交通事故次数成正比，且在凌晨和深夜时间段（0-3点）酒驾是交通事故的主要原因。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.7.4 (附加题) 分析工作日与周末在每个小时上发生的酒驾交通事故占该时段总事故数的平均比例，定义酒驾占比超过50%的时段为酒驾易发时段，构造SQL语句分析周末与非周末酒驾易发时段的差异及其主要原因（2分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 342,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "24 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>hour</th>\n",
       "        <th>avg_weekday_ratios</th>\n",
       "        <th>avg_weekend_ratios</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>0.0</td>\n",
       "        <td>49.2300</td>\n",
       "        <td>53.3200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1.0</td>\n",
       "        <td>51.5800</td>\n",
       "        <td>64.4900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2.0</td>\n",
       "        <td>54.7200</td>\n",
       "        <td>66.5200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>3.0</td>\n",
       "        <td>46.1400</td>\n",
       "        <td>63.9800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4.0</td>\n",
       "        <td>28.1900</td>\n",
       "        <td>56.1600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5.0</td>\n",
       "        <td>14.9900</td>\n",
       "        <td>42.6800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6.0</td>\n",
       "        <td>9.5600</td>\n",
       "        <td>36.8200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7.0</td>\n",
       "        <td>9.6500</td>\n",
       "        <td>30.8300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8.0</td>\n",
       "        <td>6.0500</td>\n",
       "        <td>17.4500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>9.0</td>\n",
       "        <td>6.6400</td>\n",
       "        <td>14.1200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>10.0</td>\n",
       "        <td>6.1500</td>\n",
       "        <td>14.1900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>11.0</td>\n",
       "        <td>9.2000</td>\n",
       "        <td>14.7800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>12.0</td>\n",
       "        <td>9.2600</td>\n",
       "        <td>12.5000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>13.0</td>\n",
       "        <td>10.0500</td>\n",
       "        <td>19.3900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>14.0</td>\n",
       "        <td>10.3700</td>\n",
       "        <td>18.7200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>15.0</td>\n",
       "        <td>12.6500</td>\n",
       "        <td>22.5300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>16.0</td>\n",
       "        <td>16.3200</td>\n",
       "        <td>25.9400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>17.0</td>\n",
       "        <td>17.9700</td>\n",
       "        <td>36.4500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>18.0</td>\n",
       "        <td>22.7300</td>\n",
       "        <td>35.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>19.0</td>\n",
       "        <td>26.0800</td>\n",
       "        <td>38.1300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>20.0</td>\n",
       "        <td>27.9700</td>\n",
       "        <td>38.1100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>21.0</td>\n",
       "        <td>32.2100</td>\n",
       "        <td>39.9700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>22.0</td>\n",
       "        <td>36.8800</td>\n",
       "        <td>41.7200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>23.0</td>\n",
       "        <td>42.2000</td>\n",
       "        <td>50.5300</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(0.0, Decimal('49.2300'), Decimal('53.3200')),\n",
       " (1.0, Decimal('51.5800'), Decimal('64.4900')),\n",
       " (2.0, Decimal('54.7200'), Decimal('66.5200')),\n",
       " (3.0, Decimal('46.1400'), Decimal('63.9800')),\n",
       " (4.0, Decimal('28.1900'), Decimal('56.1600')),\n",
       " (5.0, Decimal('14.9900'), Decimal('42.6800')),\n",
       " (6.0, Decimal('9.5600'), Decimal('36.8200')),\n",
       " (7.0, Decimal('9.6500'), Decimal('30.8300')),\n",
       " (8.0, Decimal('6.0500'), Decimal('17.4500')),\n",
       " (9.0, Decimal('6.6400'), Decimal('14.1200')),\n",
       " (10.0, Decimal('6.1500'), Decimal('14.1900')),\n",
       " (11.0, Decimal('9.2000'), Decimal('14.7800')),\n",
       " (12.0, Decimal('9.2600'), Decimal('12.5000')),\n",
       " (13.0, Decimal('10.0500'), Decimal('19.3900')),\n",
       " (14.0, Decimal('10.3700'), Decimal('18.7200')),\n",
       " (15.0, Decimal('12.6500'), Decimal('22.5300')),\n",
       " (16.0, Decimal('16.3200'), Decimal('25.9400')),\n",
       " (17.0, Decimal('17.9700'), Decimal('36.4500')),\n",
       " (18.0, Decimal('22.7300'), Decimal('35.0000')),\n",
       " (19.0, Decimal('26.0800'), Decimal('38.1300')),\n",
       " (20.0, Decimal('27.9700'), Decimal('38.1100')),\n",
       " (21.0, Decimal('32.2100'), Decimal('39.9700')),\n",
       " (22.0, Decimal('36.8800'), Decimal('41.7200')),\n",
       " (23.0, Decimal('42.2000'), Decimal('50.5300'))]"
      ]
     },
     "execution_count": 342,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "select ua.hour,\n",
    "round( (((select sum(count)::float from \n",
    "         (select count(*) from usaccidents ua1 where ua1.hour = ua.hour and  ua1.drunk_dr > 0 and ua1.day_week between 2 and 6)\n",
    "             s)/\n",
    "            (select count(*)::float from \n",
    "         (select day,month,year from usaccidents where day_week between 2 and 6 group by day,month,year)\n",
    "         c))/\n",
    "          ((select sum(count)::float from (select count(*) from usaccidents ua1 where ua1.hour = ua.hour and ua1.day_week between 2 and 6 ) s)/\n",
    "        (select count(*)::float from (select day,month,year from usaccidents ua1 where ua1.day_week between 2 and 6 group by day,month,year) c)))\n",
    "      ::numeric,4)*100\n",
    "avg_weekday_ratios,\n",
    "round( (((select sum(count)::float from \n",
    "         (select count(*) from usaccidents ua1 where ua1.hour = ua.hour and  ua1.drunk_dr > 0 and ua1.day_week not between 2 and 6)\n",
    "         s)/\n",
    "    (select count(*)::float from \n",
    "         (select day,month,year from usaccidents where day_week not between 2 and 6 group by day,month,year)\n",
    "         c))/\n",
    "      ((select sum(count)::float from (select count(*) from usaccidents ua1 where ua1.hour = ua.hour and ua1.day_week not between 2 and 6 ) s)/\n",
    "        (select count(*)::float from (select day,month,year from usaccidents ua1 where ua1.day_week not between 2 and 6 group by day,month,year) c)))\n",
    "      ::numeric,4)*100\n",
    "avg_weekend_ratios\n",
    "from usaccidents ua where hour between 0 and 23 group by hour order by hour;"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "结论：由上表可知，工作日酒驾易发时段为凌晨1、2点；休息日酒驾易发时段为23点至次日4点；\n",
    "酒驾易发时段主要都集中在凌晨，且休息日酒驾易发时段要明显长于工作日；\n",
    "其主要原因可能是休息日驾车参加聚会饮酒的可能性较大，且饮酒后容易晚归；\n",
    "凌晨也是驾驶最为疲劳的时间，进一步增大了饮酒后出车祸概率；\n",
    "此时除了参加聚会归来的驾驶者很少有其他驾驶者在路上行驶，因此酒驾占比较高。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.7.5 分析周末与非周末哪些高速公路上发生酒驾次数有较大差异，距离某高速公路小于500米，认为该交通事故发生在这条高速公路上，通过choroplethMap进行可视化，choroplethMap函数要求查询结果模式至少包含gid，name，geom和value属性，其中value为其内的交通事故数目（3分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "116 rows affected.\n",
      " * postgresql://postgres:***@localhost/hw2\n",
      "124 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map12' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map12.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new ChoroplethMap('map12' ,4,1)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div id='map13' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map13.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new ChoroplethMap('map13' ,4,1)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 查询非周末每条高速公路上的酒驾次数\n",
    "query1 = \"\"\"\n",
    "select hw.gid, hw.full_name as name, hw.geom, count(*) as value\n",
    "from (select geom from usaccidents where drunk_dr > 0 and day_week between 2 and 6) ua, ushighways hw \n",
    "where ST_DWithin(hw.geom::geography, ua.geom::geography,500)  \n",
    "group by hw.gid order by value;\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "result1 = %sql $query1\n",
    "\n",
    "# 查询周末每条高速公路上的酒驾次数\n",
    "query2 = \"\"\" \n",
    "select hw.gid, hw.full_name as name, hw.geom, count(*) as value\n",
    "from (select geom from usaccidents where drunk_dr > 0 and day_week not between 2 and 6) ua, ushighways hw \n",
    "where ST_DWithin(hw.geom::geography, ua.geom::geography,500)  \n",
    "group by hw.gid order by value;\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "result2 = %sql $query2\n",
    "\n",
    "choroplethMap(result1, \"map12\", 4, 1)\n",
    "choroplethMap(result2, \"map13\", 4, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "结论\n",
    "观察图表和数据，可认为周末平均每日酒驾次数和非周末平均每日酒驾次数相对误差大于0.3且绝对误差大于3为具有较大差异；\n",
    "构造以下sql语句查询周末和非周末的平均每日酒驾次数可得，16条高速公路存在较大差异：\n",
    "16条高速公路中周末平均每日酒驾次数均大于非周末平均每日酒驾次数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 354,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "16 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "        <th>weekvalue</th>\n",
       "        <th>weekendvalue</th>\n",
       "        <th>df</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 10</td>\n",
       "        <td>10.8000000000000000</td>\n",
       "        <td>23.0000000000000000</td>\n",
       "        <td>0.5304347826086956</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 95</td>\n",
       "        <td>8.0000000000000000</td>\n",
       "        <td>21.0000000000000000</td>\n",
       "        <td>0.6190476190476191</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 5</td>\n",
       "        <td>5.4000000000000000</td>\n",
       "        <td>14.0000000000000000</td>\n",
       "        <td>0.6142857142857142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 75</td>\n",
       "        <td>8.6000000000000000</td>\n",
       "        <td>13.5000000000000000</td>\n",
       "        <td>0.362962962962963</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 70</td>\n",
       "        <td>5.4000000000000000</td>\n",
       "        <td>11.5000000000000000</td>\n",
       "        <td>0.5304347826086956</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 90</td>\n",
       "        <td>4.0000000000000000</td>\n",
       "        <td>11.5000000000000000</td>\n",
       "        <td>0.6521739130434783</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 80</td>\n",
       "        <td>4.0000000000000000</td>\n",
       "        <td>11.5000000000000000</td>\n",
       "        <td>0.6521739130434783</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 20</td>\n",
       "        <td>3.2000000000000000</td>\n",
       "        <td>10.5000000000000000</td>\n",
       "        <td>0.6952380952380952</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 15</td>\n",
       "        <td>5.2000000000000000</td>\n",
       "        <td>9.5000000000000000</td>\n",
       "        <td>0.4526315789473684</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 35</td>\n",
       "        <td>4.6000000000000000</td>\n",
       "        <td>9.0000000000000000</td>\n",
       "        <td>0.48888888888888893</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 25</td>\n",
       "        <td>1.4000000000000000</td>\n",
       "        <td>8.5000000000000000</td>\n",
       "        <td>0.8352941176470587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 94</td>\n",
       "        <td>2.4000000000000000</td>\n",
       "        <td>6.5000000000000000</td>\n",
       "        <td>0.6307692307692307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 85</td>\n",
       "        <td>1.4000000000000000</td>\n",
       "        <td>5.0000000000000000</td>\n",
       "        <td>0.72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 65</td>\n",
       "        <td>1.6000000000000000</td>\n",
       "        <td>5.0000000000000000</td>\n",
       "        <td>0.6799999999999999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 240</td>\n",
       "        <td>0.40000000000000000000</td>\n",
       "        <td>4.0000000000000000</td>\n",
       "        <td>0.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>I 76</td>\n",
       "        <td>0.60000000000000000000</td>\n",
       "        <td>4.0000000000000000</td>\n",
       "        <td>0.85</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('I 10', Decimal('10.8000000000000000'), Decimal('23.0000000000000000'), 0.5304347826086956),\n",
       " ('I 95', Decimal('8.0000000000000000'), Decimal('21.0000000000000000'), 0.6190476190476191),\n",
       " ('I 5', Decimal('5.4000000000000000'), Decimal('14.0000000000000000'), 0.6142857142857142),\n",
       " ('I 75', Decimal('8.6000000000000000'), Decimal('13.5000000000000000'), 0.362962962962963),\n",
       " ('I 70', Decimal('5.4000000000000000'), Decimal('11.5000000000000000'), 0.5304347826086956),\n",
       " ('I 90', Decimal('4.0000000000000000'), Decimal('11.5000000000000000'), 0.6521739130434783),\n",
       " ('I 80', Decimal('4.0000000000000000'), Decimal('11.5000000000000000'), 0.6521739130434783),\n",
       " ('I 20', Decimal('3.2000000000000000'), Decimal('10.5000000000000000'), 0.6952380952380952),\n",
       " ('I 15', Decimal('5.2000000000000000'), Decimal('9.5000000000000000'), 0.4526315789473684),\n",
       " ('I 35', Decimal('4.6000000000000000'), Decimal('9.0000000000000000'), 0.48888888888888893),\n",
       " ('I 25', Decimal('1.4000000000000000'), Decimal('8.5000000000000000'), 0.8352941176470587),\n",
       " ('I 94', Decimal('2.4000000000000000'), Decimal('6.5000000000000000'), 0.6307692307692307),\n",
       " ('I 85', Decimal('1.4000000000000000'), Decimal('5.0000000000000000'), 0.72),\n",
       " ('I 65', Decimal('1.6000000000000000'), Decimal('5.0000000000000000'), 0.6799999999999999),\n",
       " ('I 240', Decimal('0.40000000000000000000'), Decimal('4.0000000000000000'), 0.9),\n",
       " ('I 76', Decimal('0.60000000000000000000'), Decimal('4.0000000000000000'), 0.85)]"
      ]
     },
     "execution_count": 354,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "select weekend.name, week.value as weekvalue, weekend.value as weekendvalue, (weekend.value - week.value)::float/weekend.value::float as df\n",
    "from (select hw.gid, hw.full_name as name, count(*)/5.0 as value\n",
    "from (select geom from usaccidents where drunk_dr > 0 and day_week between 2 and 6) ua, ushighways hw \n",
    "where ST_DWithin(hw.geom::geography, ua.geom::geography,500)  \n",
    "group by hw.gid) week,\n",
    "(select hw.gid, hw.full_name as name, count(*)/2.0 as value\n",
    "from (select geom from usaccidents where drunk_dr > 0 and day_week not between 2 and 6) ua, ushighways hw \n",
    "where ST_DWithin(hw.geom::geography, ua.geom::geography,500)  \n",
    "group by hw.gid) weekend \n",
    "where week.gid = weekend.gid and ( abs((weekend.value - week.value)::float/weekend.value) > 0.3 or abs((weekend.value - week.value)::float/week.value) > 0.3 )\n",
    "and ( abs(weekend.value - week.value) > 3 )\n",
    "order by weekend.value desc;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.8 (附加题) 交通事故数据分析\n",
    "\n",
    "基于交通事故数据及其他的相关数据，自行构思问题，通过SQL查询、Python编程、统计分析或可视化等，挖掘交通事故数据中隐含的模式和知识，提供安全驾驶建议。根据问题意义和分析质量，最高奖励5分"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 对数据基于月份的统计："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "12 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<BarContainer object of 12 artists>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY4AAAEXCAYAAAC6baP3AAAABHNCS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32yfm5O5CuUbI+8JikS4HTSae4nwypqwrYlXQZ2aXAT4G9SdfpMBs0PuIwa92TEfFQpEuHLiJduTFI12eYSEoi3wGIiB8Bm0jaMD/35oh4O59i4gVgsz5i3BsRS3KMB3O9ZoPKicOsdW9X7r9bWX6XfInUXp7T+MVt9bnv0PfRf7PrmXWME4dZfX5CuvZ2o9vpxVVcMOl1UteV2ZDmvRez+pwFXC5pIenyoNP7WzkiXsqD6w8DPyCdVttsyPG5qszMrIi7qszMrIgTh5mZFXHiMDOzIk4cZmZWxInDzMyKOHGYmVkRJw4zMyvixGFmZkX+P5F3DvUSODyzAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "query2 = \"\"\"\n",
    "select month,count(*) from usaccidents group by month order by month;\n",
    "\"\"\"\n",
    "result2 = %sql $query2\n",
    "%matplotlib inline\n",
    "result2.bar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "结论：秋季7-10月的事故发生概率最高，春季2月最低；\n",
    "可能是由于二月份冬季较为寒冷下雪，通行不易，出行几率减少；\n",
    "以下进一步查询在春季(2月)每公里平均车祸数和秋季（8月）平均车祸数差距较大的高速公路，\n",
    "作为事故季节性高速公路："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "58 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map22' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map22.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new ChoroplethMap('map22' ,4,1)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query1 = \"\"\"\n",
    "select c8.gid, c8.name,c8.geom, ( c8.value-c2.value ) as value\n",
    "from\n",
    "(select hw.gid, hw.full_name as name, hw.geom, count(*) as value \n",
    "from (select * from usaccidents where month = 8 ) ua, ushighways hw \n",
    "where ST_DWithin(hw.geom::geography, ua.geom::geography,500)  \n",
    "group by hw.gid) c8,\n",
    "(select hw.gid, hw.full_name as name, hw.geom, count(*) as value \n",
    "from (select * from usaccidents where month = 2 ) ua, ushighways hw \n",
    "where ST_DWithin(hw.geom::geography, ua.geom::geography,500)  \n",
    "group by hw.gid) c2\n",
    "where c2.gid = c8.gid order by value;\n",
    "\"\"\"\n",
    "result1 = %sql $query1\n",
    "choroplethMap(result1, \"map22\", 4, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 对事故造成首次伤害的事件进行统计:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "5 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>harm_ev</th>\n",
       "        <th>count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>12.0</td>\n",
       "        <td>38.04949325374619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8.0</td>\n",
       "        <td>15.48529503202139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1.0</td>\n",
       "        <td>8.776347696325312</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>42.0</td>\n",
       "        <td>7.144189516881179</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>33.0</td>\n",
       "        <td>3.3917801405210475</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(12.0, 38.04949325374619),\n",
       " (8.0, 15.48529503202139),\n",
       " (1.0, 8.776347696325312),\n",
       " (42.0, 7.144189516881179),\n",
       " (33.0, 3.3917801405210475)]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "select harm_ev,count(*)::float/(select count(*) from usaccidents)*100.0 count from usaccidents group by harm_ev order by count desc limit 5;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>sum</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>72.84710563949513</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(72.84710563949513,)]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%sql select sum(count) from (select harm_ev,count(*)::float/(select count(*) from usaccidents)*100.0 count from usaccidents group by harm_ev order by count desc limit 5) c;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAnwAAAD7CAYAAADwx0kfAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzs3Xd8VfX9+PHX+96bO0ISwt4QRUZYooKKCxxVW9M6vrVxQ9U6a2urbfOr/bb020Vr47dD62jrbLWOr7ZKrKMOtqDICBA2Ya8ECEnIvPf9++OcQAghZJ+M9/PxyCM3Z3zO+1xI8s77M46oKsYYY4wxpuPyeR2AMcYYY4xpWZbwGWOMMcZ0cJbwGWOMMcZ0cJbwGWOMMcZ0cJbwGWOMMcZ0cJbwGWOMMcZ0cJbwGWOOIiLTReRvrXCdlSIypaWv4yURuVtEdotIkYj0OMGx00RkbmvFZozpXCzhM6aTcZOPqo+YiJRU+/rGZr7WsyJSXuOa6QCqOlpVP25EmykioiISqOOYWpNW97xTGnrNxhCROOAR4FJVTVDV/Ca0VXXPRbW9ly2ltZJ/Y0zLO+4PTGNMx6SqCVWvRSQXuF1V/1Nt2/RmvuRvVPVHDTlBRAKqWtnMcTRaI+PpA4SBlc0YSnJbel+MMe2HVfiMMbUJisjzIlLodr1OqNohIv1F5P9EZK+IbBKRbzXmAiKSKyKXuK+ni8hrIvI3ETkITBORM0XkMxE56HaLPuKeOtv9fMCtck1q5PXPFJEFInJARHaKyKMiEqy2X0XkXhFZB6yrtu0eEVnnvjc/E5GhbjsHReQVEQmKyHBgTbU4P6ytMikiH4vI7Y2Jv1obZ4vILhHxV9t2tYgsd1/7RCRDRDaISL4bY3d3X1VMU0Vki4jkichD7r7LgR8C6e77vKwpcRpjvGUJnzGmNl8B/gEkA28Cj4KTPABvAcuAAcDFwP0iclk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13dPSJNkn5UXnZpb43GjjnjfvrgJRMvn7GRhwlgEXBLXhZSrKzd6MqZ2ajhgM/MBqyru2dnUrLWQ0itgO76HUYrJp28cjO9vPMAiz1PmmXhVuBWipVnhr5mZjZaOOAzs42SU71MJ02GfghpQvhWplqyFp06/pb7v7DJNfu2sOtS1rfi3U+x4plWzAxwwGdmQyyP/ZvJ+uBvJrBtWys1yo1jbe/KSSc+uYl6d6xZ/XvgQdLUZguA+RQrv2lLBc1sxHPAZ2bDrqu7ZwowKy97kVoEt2prpUaXP5414bob/nrCzatJwd1CYIXH4ZlZqxzwmdlrLj8EMgXYMy9TgZ3zurE8+0cAq4BfAsuBxcASYEW5VFjTzoqZ2ejmgM/MRoyu7p7xQBfwjrzsXPP6LdC2WSeG2tOkoO7RPl8fK5cKL7WzYmbWmRzwmdmo0NXd8zrgTcCb89e+r6vft3O84PPAb/LydIOvq8qlwu/bVkMzG5Mc8JlZR8mthFvWLFv0+b66bMb6FsPalsO+63qB1cCLTZZKuVT4w7BclJnZRnLAZ2ZmZtbhxrW7AmZmZmY2vBzwmZmZmXU4B3xmZmZmHc4Bn5mZmVmHc8BnZmZm1uEc8JmZmZl1OAd8ZmZmZh3OAZ+ZmZlZh3PAZ2ZmZtbhHPCZmZmZdTgHfGZmZmYdzgGfmZmZWYdzwGdmZmbW4RzwmZmZmXU4B3xmZmZmHc4Bn5mZmVmHc8BnZmZm1uEc8JmZmZl1OAd8ZmZmZh3OAZ+ZmZlZh3PAZ2ZmZtbhHPCZmZmZdTgHfGZmZmYdzgGfmZmZWYdzwGdmZmbW4RzwmZmZmXU4B3xmZmZmHc4Bn5mZmVmHc8BnZmZm1uEc8JmZmZl1OAd8ZmZmZh3u/wHfBEBjcFMA4gAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "labels = ['Collision with Motor Vehicle In-Transport','Collision with Pedestrian','Rollover/Overturn','Collision with Tree (Standing Only)','Collision with Curb','other']\n",
    "sizes = [38.05,15.49,8.78,7.14,3.39,27.15]\n",
    "plt.pie(sizes,labels=labels,autopct='%1.1f%%',shadow=False,startangle=150)\n",
    "plt.title(\"The First Harmful Event\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "结论：\n",
    "对事故造成首次伤害的事件进行统计，排名前5的是：\n",
    "Collision with Motor Vehicle In-Transport：38.05%\n",
    "Collision with Pedestrian：15.49%\n",
    "Rollover/Overturn：8.78%\n",
    "Collision with Tree (Standing Only)：7.14%\n",
    "Collision with Curb：3.39%\n",
    "这五种事件占据了总事件数的72.85%"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 对发生事故时的天气和光照进行统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "5 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>weather</th>\n",
       "        <th>count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1.0</td>\n",
       "        <td>71.20562084188273</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>10.0</td>\n",
       "        <td>17.039731393396753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2.0</td>\n",
       "        <td>7.6602623888578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5.0</td>\n",
       "        <td>1.2497668345457937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4.0</td>\n",
       "        <td>0.9886215258347323</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(1.0, 71.20562084188273),\n",
       " (10.0, 17.039731393396753),\n",
       " (2.0, 7.6602623888578),\n",
       " (5.0, 1.2497668345457937),\n",
       " (4.0, 0.9886215258347323)]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "select weather,count(*)::float/(select count(*) from usaccidents)*100.0 count from usaccidents where weather != 0 group by weather order by count desc limit 5;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "对发生事故时的天气进行统计，取数量最多的前5位：\n",
    "Clear：71.20%\n",
    "Cloudy：17.04%\n",
    "Rain：7.66%\n",
    "Fog, Smog, Smoke：1.25%\n",
    "Snow：0.98%\n",
    "结论：绝大多数的事故都发生在天气良好的情况下；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "5 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>lgt_cond</th>\n",
       "        <th>count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1.0</td>\n",
       "        <td>47.590623639868184</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2.0</td>\n",
       "        <td>28.237890940744887</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>3.0</td>\n",
       "        <td>18.401417645961573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5.0</td>\n",
       "        <td>2.471553814586831</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4.0</td>\n",
       "        <td>2.0332027606789778</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(1.0, 47.590623639868184),\n",
       " (2.0, 28.237890940744887),\n",
       " (3.0, 18.401417645961573),\n",
       " (5.0, 2.471553814586831),\n",
       " (4.0, 2.0332027606789778)]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "select lgt_cond,count(*)::float/(select count(*) from usaccidents)*100.0 count from usaccidents where lgt_cond != 9 group by lgt_cond order by count desc limit 5;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "结论：大多数的事故都发生在光照良好的情况下；"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 对发生事故的道路种类进行统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "3 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>route</th>\n",
       "        <th>the route signing of the trafficway</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>3.0</td>\n",
       "        <td>30.528394741954283</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6.0</td>\n",
       "        <td>17.807420837920095</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2.0</td>\n",
       "        <td>16.755164152042997</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(3.0, 30.528394741954283),\n",
       " (6.0, 17.807420837920095),\n",
       " (2.0, 16.755164152042997)]"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%sql select route,count(*)::float/(select count(*)  from usaccidents where route != 9)*100.0  \"the route signing of the trafficway\" from usaccidents where route != 9 group by route order by \"the route signing of the trafficway\" desc limit 3;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "8 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<BarContainer object of 8 artists>"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAESCAYAAADwnNLKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAHDpJREFUeJzt3XmcHXWd7vHPQwgEIQqSZhGEBCaAwECAiF7xgoJBRTb34bowgIYRURRU0JcLqHcEF3S8dxgFUXBBBxAHkE0uIAijQIAAwYBRDIggRGRXlpDn/lHVeGi6T1cvVdXJed6v13mdU3XqnHr6VHd/T9Wv6veTbSIionet1HaAiIhoVwpBRESPSyGIiOhxKQQRET0uhSAioselEERE9LgUgoiIHpdCEBHR41IIIiJ63MptB6hi2rRpnj59etsxIiKWK9ddd92fbfcNt9xyUQimT5/OvHnz2o4REbFckXRHleVyaCgioselEERE9LgUgoiIHpdCEBHR41IIIiJ6XApBRESPSyGIiOhxKQQRET1uubigbCymH3Vea+tefOwbWlt3RERVte0RSJoi6RpJN0q6RdIx5fwZkq6WtEjSf0papa4MERExvDoPDT0B7Gp7W2AW8DpJLweOA75qeybwAHBQjRkiImIYtRUCFx4tJyeXNwO7AmeW808F9q0rQ0REDK/WxmJJkyTNB+4DLgZ+Bzxoe2m5yF3ABkO8dq6keZLmLVmypM6YERE9rdZCYPtp27OADYEdgZcMttgQrz3R9mzbs/v6hu1FNSIiRqmR00dtPwj8HHg5sKak/rOVNgTubiJDREQMrs6zhvokrVk+Xg14DbAQuAx4S7nY/sDZdWWIiIjh1XkdwfrAqZImURSc023/VNKvgR9J+jxwA3ByjRkiImIYtRUC2zcB2w0y/3aK9oKIiJgA0sVERESPSyGIiOhxKQQRET0uhSAioselEERE9LhhC4GkQyWt1USYiIhoXpU9gvWAayWdLul1klR3qIiIaM6whcD2J4GZFBd+/TOwSNK/Stq05mwREdGASm0Etg38qbwtBdYCzpT0xRqzRURE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gIMk3QjcAuzTVP6IqtLXUEREj8seQUREj0shiIjocSkEERE9LoUgIqLHpRBERPS4FIKIiB6XQhAR0eNSCCIietz/B0xOMOtj/LcJAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query2 = \"\"\"\n",
    "select route,count(*)::float/(select count(*)  from usaccidents where route != 9)*100.0  \"the route signing of the trafficway\" from usaccidents where route != 9 group by route order by \"the route signing of the trafficway\" desc;\n",
    "\"\"\"\n",
    "result2 = %sql $query2\n",
    "%matplotlib inline\n",
    "result2.bar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1 Interstate\n",
    "2 U.S. Highway\n",
    "3 State Highway\n",
    "4 County Road\n",
    "5 Local Street - Township\n",
    "6 Local Street - Municipality\n",
    "7 Local Street - Frontage Road\n",
    "8 Other\n",
    "结论：\n",
    "发生事故前三位的道路种类为:\n",
    "State Highway  30.53%\n",
    "Local Street - Municipality 17.81%\n",
    "U.S. Highway 16.7%\n",
    "发生在高速公路上的事故总数量是最多的；"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 对事故涉及车数目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "5 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>ve_total</th>\n",
       "        <th>the route signing of the trafficway</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1.0</td>\n",
       "        <td>56.177330100105706</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2.0</td>\n",
       "        <td>36.11266554747249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>3.0</td>\n",
       "        <td>5.642603991792576</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4.0</td>\n",
       "        <td>1.2995087981098055</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5.0</td>\n",
       "        <td>0.36995585400733694</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(1.0, 56.177330100105706),\n",
       " (2.0, 36.11266554747249),\n",
       " (3.0, 5.642603991792576),\n",
       " (4.0, 1.2995087981098055),\n",
       " (5.0, 0.36995585400733694)]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "select ve_total,count(*)::float/(select count(*)  from usaccidents)*100.0  \"the route signing of the trafficway\" from usaccidents  group by ve_total order by ve_total limit 5;\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "结论：单车驾驶产生的事故占大多数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 分别统计每条高速公路上紧急救援到达的平均小时数并绘图：\n",
    "   - Arrival Time (Hour) EMS is the time Emergency Medical Service arrived on the crash scene.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "32 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map20' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map20.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new ChoroplethMap('map20' ,4,1)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query1 = \"\"\"\n",
    "select hw.gid, hw.full_name as name,hw.geom, avg(ua.d_hour)  as value,count(*) as c\n",
    "from ( select (arr_hour - hour) as d_hour,geom from usaccidents where arr_hour != 99 and arr_hour != 88 and hour != 99 and arr_hour - hour > 0 ) ua, ushighways hw \n",
    "where ST_DWithin(hw.geom::geography, ua.geom::geography,500)\n",
    "group by hw.gid having count(*) > 3 order by value desc;\n",
    "\n",
    "\"\"\"\n",
    "result1 = %sql $query1\n",
    "choroplethMap(result1, \"map20\", 4, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "结论：可以认为平均医疗救援时长越大的高速公路，在出车祸时伤员得不到及时的救助的可能性越高，即危险性越高；选取平均医疗救援时长排名前十的高速公路作为在医疗救援方面风险较高的高速公路；\n",
    "（忽略可统计次数过少的高速公路）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "10 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>gid</th>\n",
       "        <th>name</th>\n",
       "        <th>value</th>\n",
       "        <th>c</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>139</td>\n",
       "        <td>I 55</td>\n",
       "        <td>2.0</td>\n",
       "        <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>130</td>\n",
       "        <td>I 5</td>\n",
       "        <td>1.75</td>\n",
       "        <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>220</td>\n",
       "        <td>I 95</td>\n",
       "        <td>1.5</td>\n",
       "        <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>77</td>\n",
       "        <td>I 35</td>\n",
       "        <td>1.3333333333333333</td>\n",
       "        <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>175</td>\n",
       "        <td>I 70</td>\n",
       "        <td>1.303030303030303</td>\n",
       "        <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>97</td>\n",
       "        <td>I 40</td>\n",
       "        <td>1.28</td>\n",
       "        <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>178</td>\n",
       "        <td>I 71</td>\n",
       "        <td>1.2727272727272727</td>\n",
       "        <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>200</td>\n",
       "        <td>I 81</td>\n",
       "        <td>1.25</td>\n",
       "        <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>189</td>\n",
       "        <td>I 77</td>\n",
       "        <td>1.1428571428571428</td>\n",
       "        <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>158</td>\n",
       "        <td>I 65</td>\n",
       "        <td>1.0833333333333333</td>\n",
       "        <td>12</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(139, 'I 55', 2.0, 9),\n",
       " (130, 'I 5', 1.75, 8),\n",
       " (220, 'I 95', 1.5, 20),\n",
       " (77, 'I 35', 1.3333333333333333, 15),\n",
       " (175, 'I 70', 1.303030303030303, 33),\n",
       " (97, 'I 40', 1.28, 25),\n",
       " (178, 'I 71', 1.2727272727272727, 11),\n",
       " (200, 'I 81', 1.25, 8),\n",
       " (189, 'I 77', 1.1428571428571428, 14),\n",
       " (158, 'I 65', 1.0833333333333333, 12)]"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "select hw.gid, hw.full_name as name, avg(ua.d_hour)  as value,count(*) as c\n",
    "from ( select (arr_hour - hour) as d_hour,geom from usaccidents where arr_hour != 99 and arr_hour != 88 and hour != 99 and arr_hour - hour > 0 ) ua, ushighways hw \n",
    "where ST_DWithin(hw.geom::geography, ua.geom::geography,500)\n",
    "group by hw.gid having count(*) > 4 order by value desc limit 10;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 查询平均每公里事故数最多的高速公路：\n",
    "类似 3.6.12 将交通事故与高速公路基于空间距离进行关联，即距离某高速公路小于500米，认为该交通事故发生在这条高速公路上；查询哪条高速公路上的交通事故最多。使用ST_DWithin加速距离判断，同时仅考虑在8月和9月发生的交通事故。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "130 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div id='map21' style=\"width: 1000px; height: 400px\"></div>\n",
       "    <link rel=\"stylesheet\" href=\"tools/leaflet.css\">\n",
       "    <link rel=\"stylesheet\" href=\"tools/map.css\">\n",
       "        <script src=\"tools/d3.min.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/leaflet-heat.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/jquery-3.1.1.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/L.D3SvgOverlay.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"jsonData/map21.json\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/wkx.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/underscore.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/geom_display.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/baseMap.js\" type=\"text/javascript\"></script>\n",
       "        <script src=\"tools/heatMap.js\" type=\"text/javascript\"></script>\n",
       "\t    <script src=\"tools/choroplethMap.js\" type=\"text/javascript\"></script>\n",
       "         <script type=\"text/javascript\"> \n",
       "        (new ChoroplethMap('map21' ,4,1)).display();</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query1 = \"\"\"\n",
    "select hw.gid, hw.full_name as name, hw.geom, count(*)/( ST_Length(hw.geom::geography) /1000.0 ) as value from (select * from usaccidents where month between 8 and 9) ua, ushighways hw \n",
    "where ST_DWithin(hw.geom::geography, ua.geom::geography,500)  \n",
    "group by hw.gid order by value ;\n",
    "\"\"\"\n",
    "result1 = %sql $query1\n",
    "choroplethMap(result1, \"map21\", 4, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "结论：观察图可知，平均每公里事故数最多的高速公路\n",
    "多位于两个高速公路连接处的次级高速公路；"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 使用apriori算法进行数据关联分析：\n",
    "选取 ve_total,hour,route,lgt_cond,weather1,harm_ev 等项；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "对所有数据进行数据关联分析："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/hw2\n",
      "32166 rows affected.\n",
      "32166\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "query1 = \"\"\"\n",
    "select ve_total,hour,route,lgt_cond,weather1,harm_ev from usaccidents;\n",
    "\"\"\"\n",
    "index = [\"ve_total\",\"hour\",\"route\",\"lgt_cond\",\"weather1\",\"harm_ev\"]\n",
    "result1 = %sql $query1\n",
    "df = pd.DataFrame(result1)\n",
    "print(len(df.index))\n",
    "for i in range(0,len(index)):\n",
    "    df[i] = df[i].map(lambda x: str(x)+\"_\"+index[i])\n",
    "from mlxtend.preprocessing import TransactionEncoder\n",
    "def deal(data):\n",
    "    return data.dropna().tolist()\n",
    "df_arr = df.apply(deal,axis=1).tolist()\t\n",
    "te = TransactionEncoder()\t# 定义模型\n",
    "df_tf = te.fit_transform(df_arr)\n",
    "df = pd.DataFrame(df_tf,columns=te.columns_)\n",
    "\n",
    "from mlxtend.frequent_patterns import apriori\n",
    "frequent_itemsets = apriori(df,min_support=0.005,use_colnames=True)\t# use_colnames=True表示使用元素名字，默认的False使用列名代表元素\n"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "iVBORw0KGgoAAAANSUhEUgAAAgMAAAASCAYAAADGzt1AAAAJWUlEQVR4Ae2bjU3fPBDG826AGAF1AsQIqBMgRkBMgBihYoKqI6BOUHWEqhNUjIDYgFc/10/+F9dOLl//j2AksOOcz3f3PDlfgtw09acYgdfX1/evX7++FwU2fAO/8X/DLh6la5VzlXP7IuYaXKt5Yz30lsRrEKc/f/68f/nypW4AEc+7u7sPGwuId3Nz82H9X++R7tdcOVc518+Q5e6uwbWaN5bDJ9W0JF69OD08PLyfnZ192DfhNPAURb9+/cpuhgQSYIhX0zTv19fXnbfo5+fnMH5xcRHkkE/17+Oa4i4SKNhDP2eLZPDjx48fra30a3G4PFLgkuNW5VzTVM4ty7dDcK1iOB1DL15zczsW9uLE5sUi013ZzszLy8vBOMR/IfwTM2JIcXUs0dBmnysEsJFNCGLk7PXEITevjpUjQNEVudMR8sS6cq4TsnoxEIFDcc3D5QHTP+TtsXjNye0EOIsTG0V80/2QIFinS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YkK8ONb2+fPn1vbSXMbZtPDH8qJPfol7+Iit9sdiwTgY6fgpmPMjHkoWGcbAgh/5b4+8emQoBMAe3mlNrREUx898xCkd131anhWOc4ofqR3I2PmVc7tcZOO4dH+LfDs018bmDfKU3VdKGG8RK3wdixdz5uZ2dIzFqYTLUY9TGIypTK0zBHnK27fVsVYf29bSndPb98UmJ39MY+Cvwq5kl0emNHfJ8cq5XTRPlXMeLnlkdpFYp7cPrh07hh4cPDLrINTVuiZex45TNxITr3ByaCPoU00xoU+6fXJbvkcVPieGh44Nnx6HMPTI7MuPyrm/XwFPlXMeLnlk9sG3Nbl2CnnDg4NHZh9YscYaeJ0CTqPjS6BshcMnF3s9WmGckPk/9VRVJzePavSU/Y//4+/9iuKR2TdwpxzzubE6Zc55uOSRmRvDMfPX4NopYOjBwSMzJtZLyC6J1yngNClmbP4UBLT8D4T+JEXJJAJ2qm8piSujL/Eb/0dPPIIJVLyxGAwFTc4Pj8whXKmcOz3Oebjkkdk339bg2rHnDQ8OHpl9Y8V6S+KVw+l/vknDCrQkKpwAAAAASUVORK5CYII="
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 选取最小支持率为0.001；\n",
    "- 以杠杆率排序：\n",
    "![image.png](attachment:image.png)\n",
    "- 去除置信度小于0.6的项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "from mlxtend.frequent_patterns import association_rules\n",
    "association_rule = association_rules(frequent_itemsets,metric='confidence',min_threshold=0.6)\n",
    "association_rule.sort_values(by='leverage',ascending=False,inplace=True) \n",
    "#print(association_rule.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "8\n",
      "3\n",
      "                                            antecedents    consequents  \\\n",
      "1450            (1.0_ve_total, 20.0_hour, 3.0_lgt_cond)  (8.0_harm_ev)   \n",
      "1981  (1.0_ve_total, 3.0_lgt_cond, 1.0_weather1, 20....  (8.0_harm_ev)   \n",
      "1431            (1.0_ve_total, 19.0_hour, 3.0_lgt_cond)  (8.0_harm_ev)   \n",
      "\n",
      "      antecedent support  consequent support   support  confidence      lift  \\\n",
      "1450            0.012684            0.154853  0.007928    0.625000  4.036087   \n",
      "1981            0.009575            0.154853  0.006062    0.633117  4.088504   \n",
      "1431            0.008580            0.154853  0.005689    0.663043  4.281762   \n",
      "\n",
      "      leverage  conviction  \n",
      "1450  0.005963    2.253725  \n",
      "1981  0.004580    2.303587  \n",
      "1431  0.004361    2.508178  \n",
      "\n",
      "\n",
      "\n",
      "12\n",
      "205\n",
      "                                     antecedents     consequents  \\\n",
      "110                               (2.0_ve_total)  (12.0_harm_ev)   \n",
      "566                 (1.0_weather1, 2.0_ve_total)  (12.0_harm_ev)   \n",
      "288                 (1.0_lgt_cond, 2.0_ve_total)  (12.0_harm_ev)   \n",
      "1011  (1.0_lgt_cond, 1.0_weather1, 2.0_ve_total)  (12.0_harm_ev)   \n",
      "794                    (3.0_route, 2.0_ve_total)  (12.0_harm_ev)   \n",
      "\n",
      "      antecedent support  consequent support   support  confidence      lift  \\\n",
      "110             0.361127            0.380495  0.315893    0.874742  2.298958   \n",
      "566             0.263788            0.380495  0.231269    0.876724  2.304166   \n",
      "288             0.210004            0.380495  0.189703    0.903331  2.374094   \n",
      "1011            0.156034            0.380495  0.141205    0.904961  2.378379   \n",
      "794             0.118386            0.380495  0.107287    0.906250  2.381766   \n",
      "\n",
      "      leverage  conviction  \n",
      "110   0.178486    4.945822  \n",
      "566   0.130899    5.025335  \n",
      "288   0.109798    6.408510  \n",
      "1011  0.081835    6.518440  \n",
      "794   0.062242    6.608054  \n"
     ]
    }
   ],
   "source": [
    "#对每一种事故类型分别获取关联分析结果\n",
    "for i in range(1,75):\n",
    "    pdf = association_rule[association_rule[\"consequents\"] == frozenset({str(i)+'.0_harm_ev'})]\n",
    "    if pdf.shape[0] != 0:\n",
    "        print('\\n\\n')\n",
    "        print(i)\n",
    "        print(pdf.shape[0])\n",
    "        print(pdf.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "结论：从上述可得：\n",
    "事故种类中绝大部分汽车相互碰撞还是在晴天、光照良好条件下发生的,因此不可因为视野和天气条件好就掉以轻心；\n",
    "在州高速公路上的汽车相互碰撞占据了大部分；\n",
    "在刚入夜的时候（19时至20时）撞击行人的概率较大；"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 作业感想\n",
    "\n",
    "收获:-)，疑惑:-|，吐槽:-(，...，你的反馈很重要"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "感觉分析还不是很完善...\n",
    "应该还可以对某些特别的事件分别进行数据关联分析，如酒驾、校车事故等"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
